Check if value isset and null
IIRC, you can use get_defined_vars()
for this:
$foo = NULL;
$vars = get_defined_vars();
if (array_key_exists('bar', $vars)) {}; // Should evaluate to FALSE
if (array_key_exists('foo', $vars)) {}; // Should evaluate to TRUE
PHP check if False or Null
For returns from functions, you use neither isset
nor empty
, since those only work on variables and are simply there to test for possibly non-existing variables without triggering errors.
For function returns checking for the existence of variables is pointless, so just do:
if (!my_function()) {
// function returned a falsey value
}
To read about this in more detail, see The Definitive Guide To PHP's isset
And empty
.
Are isset() and !== null equivalent?
From PHP Manual:
isset — Determine if a variable is set and is not NULL
isset Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
http://php.net/manual/en/function.isset.php
The function call overhead is so small you probably don't need to worry about it.
Read this post: Why are PHP function calls *so* expensive?
Note that isset is not a function (it has a special opcode for it), so it's faster.
Check if array value isset and is null
Use array_key_exists()
instead of isset()
, because isset()
will return false
if the variable is null
, whereas array_key_exists()
just checks if the key exists in the array:
function check($array, $key)
{
if(array_key_exists($key, $array)) {
if (is_null($array[$key])) {
echo $key . ' is null';
} else {
echo $key . ' is set';
}
}
}
Will isset() return false if I assign NULL to a variable?
bool isset ( mixed $var [, mixed $var [, $... ]] )
Determine if a variable is set and is not NULL.
If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.
Return values
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
From the manual. Examples on the same page.
In where shall I use isset() and !empty()
isset vs. !empty
FTA:
"isset() checks if a variable has a
value including (False, 0 or empty
string), but not NULL. Returns TRUE
if var exists; FALSE otherwise.On the other hand the empty() function
checks if the variable has an empty
value empty string, 0, NULL or
False. Returns FALSE if var has a
non-empty and non-zero value."
Check for multiple null isset values
For that you can use relational operators. AND (&&) OR (||)
By using AND (&&) operators.
if ( (!isset($myarray['dataone']) || (!isset$myarray['datatwo'] ))
{
echo 'false';
}
else
{
echo 'true';
}
By using OR ( || ) operators.
if (isset($myarray['dataone'] && isset$myarray['datatwo'])
{
echo 'false';
}
else
{
echo 'true';
}
Why check both isset() and !empty()
This is completely redundant. empty
is more or less shorthand for !isset($foo) || !$foo
, and !empty
is analogous to isset($foo) && $foo
. I.e. empty
does the reverse thing of isset
plus an additional check for the truthiness of a value.
Or in other words, empty
is the same as !$foo
, but doesn't throw warnings if the variable doesn't exist. That's the main point of this function: do a boolean comparison without worrying about the variable being set.
The manual puts it like this:
empty()
is the opposite of(boolean) var
, except that no warning is generated when the variable is not set.
You can simply use !empty($vars[1])
here.
isset($_GET(var)) is true even if var is null or undefined
A sample code show difference between isset
and empty
<?php
$a = array('blank'=>'');
var_dump(isset($a['blank'])); // bool(true)
var_dump(!empty($a['blank'])); // bool(false)
Related Topics
How to Run a Cronjob Every X Minutes
Composer Update Failed -- Out of Memory
Design Patterns: How to Create Database Object/Connection Only When Needed
How to Set Cron Job Url for Codeigniter
Accessing an Associative Array by Integer Index in PHP
Getting Relative Path from Absolute Path in PHP
Phpmailer: Reply Using Only "Reply To" Address
Using MySQL Concat() in Where Clause
Curl Error 60: Ssl Certificate in Laravel 5.4
Programmatically Creating New Order in Woocommerce
How to Use Break or Continue Within for Loop in Twig Template
How to Alias a Function in PHP
Email Validation Using Regular Expression in PHP
Create an Order Programmatically with Line Items in Woocommerce 3+
Codeigniter: Try Catch Is Not Working in Model Class
How to Set a Class Attribute to a Symfony2 Form Input