Php mysql if variable is null

PHP check if False or Null

I also get confused how to check if a variable is false / null when returned from a function. When to use empty() and when to use isset() to check the condition ?

7 Answers 7

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:

@deceze, if empty is not a function, it’d be nice if it didn’t pretend one. And it does and it confuses programmers, just as a lot of things does (see me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design)

Note that this also catches empty strings and empty arrays and 0 and ‘0’, which might not be what you want

Checking variable ( a few examples )

if(is_null($x) === true) // null if($x === null) // null if($x === false) if(isset($x) === false) // variable undefined or null if(empty($x) === true) // check if variable is empty (length of 0) 

Your comment on empty not quite correct: empty also returns true for values that are equivalent to 0.

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.

ISSET checks the variable to see if it has been set, in other words, it checks to see if the variable is any value except NULL or not assigned a value . ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a » «, 0, «0», or FALSE are set, and therefore are TRUE for ISSET.

EMPTY checks to see if a variable is empty. Empty is interpreted as: » » (an empty string), 0 (0 as an integer), 0.0 (0 as a float), «0» (0 as a string), NULL, FALSE, array() (an empty array), and «$var;» (a variable declared, but without a value in a class.

isset — Determine if a variable is set and is not NULL

$a = "test"; $b = "anothertest"; var_dump(isset($a)); // TRUE var_dump(isset($a, $b)); // TRUE unset ($a); var_dump(isset($a)); // FALSE 

empty — Determine whether a variable is empty

 // Evaluates as true because $var is set if (isset($var)) < echo '$var is set even though it is empty'; >?> 

It is important to use the correct function / notation, not just whatever appears to work correctly. There are a few things to consider that are not mentioned in the existing answers.

  • Use isset to check if a variable has either not been set or has been set to null .
  • Use empty to check if a variable == false. null is cast to false and as with isset, no notice is thrown if the variable has not been set.

if (!$variable) or if ($variable == false) is the same as empty , except that a notice will be thrown if the variable has not been set.

if ($variable !== null) is the same as isset , except that a notice will be thrown if the variable has not been set.

if (!$variable) and if ($variable !== null) perform better than their respective functions but not when notices are being generated, therefore, $variable needs to have been set. Don’t suppress notices as a micro-optimisation, as this will make your code harder to debug and even suppressed notices cause a performance penalty.

Coalescing operators

If you are checking a variable so that you can assign a value to it, then you should use ?? , ?: instead of if statements.

?? assigns a value when not equal to null. $variable = $a ?? $b is the same as:

if (isset($a)) $variable = $a; else $variable = $b; 

?: assigns a value when not == to false. $variable = $a ?: $b is the same as:

if ($a) $variable = $a; else $variable = $b; 

but remember that a notice will be generated when $a has not been set. If $a might not have been set, you can use $variable = !empty($a) ? $a : $b; instead.

Источник

is_null

Finds whether the given variable is null .

Parameters

The variable being evaluated.

Return Values

Returns true if value is null , false otherwise.

Examples

Example #1 is_null() example

$foo = NULL ;
var_dump ( is_null ( $inexistent ), is_null ( $foo ));

Notice: Undefined variable: inexistent in . bool(true) bool(true)

See Also

  • The null type
  • isset() — Determine if a variable is declared and is different than null
  • is_bool() — Finds out whether a variable is a boolean
  • is_numeric() — Finds whether a variable is a number or a numeric string
  • is_float() — Finds whether the type of a variable is float
  • is_int() — Find whether the type of a variable is integer
  • is_string() — Find whether the type of a variable is string
  • is_object() — Finds whether a variable is an object
  • is_array() — Finds whether a variable is an array

User Contributed Notes 9 notes

Micro optimization isn’t worth it.

You had to do it ten million times to notice a difference, a little more than 2 seconds

$a===NULL; Took: 1.2424390316s
is_null($a); Took: 3.70693397522s

difference = 2.46449494362
difference/10,000,000 = 0.000000246449494362

The execution time difference between ===NULL and is_null is less than 250 nanoseconds. Go optimize something that matters.

See how php parses different values. $var is the variable.

strlen($var) = 0 0 1 1 1
is_null($var) = TRUE FALSE FALSE FALSE FALSE
$var == «» = TRUE TRUE TRUE FALSE FALSE
!$var = TRUE TRUE TRUE TRUE FALSE
!is_null($var) = FALSE TRUE TRUE TRUE TRUE
$var != «» = FALSE FALSE FALSE TRUE TRUE
$var = FALSE FALSE FALSE FALSE TRUE

In PHP 7 (phpng), is_null is actually marginally faster than ===, although the performance difference between the two is far smaller.

PHP 5.5.9
is_null — float(2.2381200790405)
=== — float(1.0024659633636)
=== faster by ~100ns per call

PHP 7.0.0-dev (built: May 19 2015 10:16:06)
is_null — float(1.4121870994568)
=== — float(1.4577329158783)
is_null faster by ~5ns per call

A quick test in 2022 on PHP 8.1 confirms there is still no need to micro-optimize NULL checks:

// Comparison Operator
$before = microtime ( true );
$var = null ;
for ( $i = 0 ; $i < 1000000000 ; $i ++) if( $var === null ) <>
>
$after = microtime ( true );
echo ‘ ===: ‘ . ( $after — $before ) . » seconds\n» ;

// Function
$before = microtime ( true );
$var = null ;
for ( $i = 0 ; $i < 1000000000 ; $i ++) if( is_null ( $var )) <>
>
$after = microtime ( true );
echo ‘is_null: ‘ . ( $after — $before ) . » seconds\n» ;

// ===: 4.1487579345703 seconds
// is_null: 4.1316878795624 seconds

For what I realized is that is_null($var) returns exactly the opposite of isset($var) , except that is_null($var) throws a notice if $var hasn’t been set yet.

the following will prove that:

$quirks = array( null , true , false , 0 , 1 , » , «\0» , «unset» );

foreach( $quirks as $var ) if ( $var === «unset» ) unset( $var );

echo is_null ( $var ) ? 1 : 0 ;
echo isset( $var ) ? 1 : 0 ;
echo «\n» ;
>

?>

this will print out something like:

10 // null
01 // true
01 // false
01 // 0
01 // 1
01 // »
01 // «\0»
Notice: Undefined variable: var in /srv/www/htdocs/sandbox/null/nulltest.php on line 8
10 // (unset)

For the major quirky types/values is_null($var) obviously always returns the opposite of isset($var), and the notice clearly points out the faulty line with the is_null() statement. You might want to examine the return value of those functions in detail, but since both are specified to return boolean types there should be no doubt.

A second look into the PHP specs tells that is_null() checks whether a value is null or not. So, you may pass any VALUE to it, eg. the result of a function.
isset() on the other hand is supposed to check for a VARIABLE’s existence, which makes it a language construct rather than a function. Its sole porpuse lies in that checking. Passing anything else will result in an error.

Knowing that, allows us to draw the following unlikely conclusion:

isset() as a language construct is way faster, more reliable and powerful than is_null() and should be prefered over is_null(), except for when you’re directly passing a function’s result, which is considered bad programming practice anyways.

Using === NULL instead of is_null(), is actually useful in loaded server scenarios where you have hundreds or thousands of requests per second. Saving microseconds on a lot of «simple» operations in the entire PHP execution chain usually results in being able to serve more pages per second at the same speed, or lowering your cpu usage. People usually write very bad and slow code.

$var===NULL is much faster than is_null($var) (with the same result)

I did some benchmarking with 10 million iterations:

$a=null;
isset($a); Took: 1.71841216087s
$a==NULL; Took: 1.27205181122s
$a===NULL; Took: 1.2424390316s
is_null($a); Took: 3.70693397522s
$a=5;
isset($a); Took: 1.15165400505s
$a==NULL; Took: 1.41901302338s
$a===NULL; Took: 1.21655392647s
is_null($a); Took: 3.78501200676s
error_reporting(E_ALL&~E_NOTICE);
unset($a);
isset($a); Took: 1.51441502571s
$a==NULL; Took: 16.5414860249s
$a===NULL; Took: 16.1273870468s
is_null($a); Took: 23.1918480396s

Please note, that isset is only included because it gives a good performance in any case; HOWEVER isset is NOT the same, or the opposite.
But you might be able to use isset() instead of null-checking.

You should not use is_null, except when you need a callback-function, or for conformity with is_int, is_float, etc.

I’ve found that for HTTP requests such as POST, is_null isn’t the most reliable choice for checking if empty.

if(trim($var) == «») // do something
>

I think the request does something to the input that makes it definitely not NULL.

Regarding avoidance of NULLs in your MySQL queries, why not use IS NULL and IS NOT NULL in your WHERE clauses.

SELECT *
FROM someDatabase
WHERE someAttribute IS NOT NULL

Источник

check if variable empty

If you want to test whether a variable is really NULL , use the identity operator:

$user_id === NULL // FALSE == NULL is true, FALSE === NULL is false is_null($user_id) 

If you want to check whether a variable is not set:

Or if the variable is not empty, an empty string, zero, .

If you want to test whether a variable is not an empty string, ! will also be sufficient:

Also note that because he is using == above and not === , all that is effectively being done is if (!$var || !$var .

Yep, documentation at: isset , is_null , empty . Judging by the code, I assume that the variables have to be a string. So, I recommend: if (!is_string($user_id) || $user_id === ») .

You can check if it’s not set (or empty) in a number of ways.

if ($var === null) < >// This checks if the variable, by type, IS null. 

You can check if it’s declared with:

Take note that PHP interprets 0 (integer) and «» (empty string) and false as «empty» — and dispite being different types, these specific values are by PHP considered the same. It doesn’t matter if $var is never set/declared or if it’s declared as $var = 0 or $var = «». So often you compare by using the === operator which compares with respect to data type. If $var is 0 (integer), $var == «» or $var == false will validate, but $var === «» or $var === false will not.

here i have explained how the empty function and isset works please use the one that is appropriate also you can use is_null function also

 //evaluates to true because $VAR IS SET if (isset($val)) < echo '$val is set even though it is empty'; >?> 

empty() is a little shorter, as an alternative to checking !$user_id as suggested elsewhere:

if (empty($user_id) || empty($user_name) || empty($user_logged))

To check for null values you can use is_null() as is demonstrated below.

Please define what you mean by «empty».

The test I normally use is isset() .

you can use isset() routine .

also additionaly you can refer an range of is_type () functions like

is_string(), is_float(),is_int() etc to further specificaly test

if(!($user_id || $user_name || $user_logged)) < //do your stuff >

2 . No. I actually did not understand why you write such a construct.

3 . Put all values into array, for example $ar[«user_id»], etc.

 elseif (empty($var)) < echo 'Variable is empty' . PHP_EOL; >> check($nothing); check($something); check($array); 

Its worth noting — and I only found this out after nearly 9 years of PHP coding that the best way of checking any variable exists is using the empty() function. This is because it doesn’t generate errors and even though you turn them off — PHP still generates them! empty() however won’t return errors if the variable doesn’t exist. So I believe the correct answer is not to check if its null but to do the following

variable is considered empty if it does not exist or if its value equals FALSE

As opposed to being null which is handy here!

Источник

Читайте также:  Commands python module command
Оцените статью