|
is_null
Finds whether a variable is NULL
(PHP 4 >= 4.0.4, PHP 5)
Code Examples / Notes » is_nulljacob
The notes from the other users are confusing on what happens when a value from a mysql query is NULL (SQL NULL). After doing a few tests, it looks like the way to test if the value is NULL or not is to use is_null($whatever) also, it looks like this is functionally the same as (! isset($whatever) ) when you set $whatever =NULL, isset($whatever) returns false now... is ! isset($whatever) == is_null($whatever) for all versions of php? btw, the reason why people want to check for the NULL in the application is when they use a query that returns multiple rows of multiple columns where some columns are NULL is some rows, but not others and they don't want to have multiple SELECT queries for each possible outcome. galardi
The following function is useful for set a variable to a default value the case it contains null: function Nz($TestVar,$ValueIfNull) { if (is_null($TestVar)) return $ValueIfNull; else return $TestVar; } I have used it to ensure to always write something in the cells of a table; otherwise Netscape doesn't show the color of cell background. Ex: <TD valign="top" BGCOLOR="#F0F0F0"> <?echo Nz($row2["Name"]," ");?> </TD> dstone
The above example is a little wrong. An empty value is not equivalent to null in mysql. To get not null in mysql use "... where column is not null" If you are doing a left join in mysql, no matched entry returns a null, where if you had a matched entry and were asking about a text field, '' might match, so != '' is different than is not null jbeninger
Regarding the function that returns a default value if the passed value is null: I like my functions names to bear resemblance to their function (Nz doesn't really pass that test). I've got a similar function but have called it "if_null" - after the SQL function that serves the same purpose. michael
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 Cheers, Michael marsik
I've tested different values in order to compare 'em with NULL with the help of different operators... <?php $arr=array(0, 0.0, '0', '0.0', '',FALSE,'false',NULL, 'NULL'); for ($i=0; $i<count($arr); $i++) $arr[$i]=array( $arr[$i], ((integer)($arr[$i]==NULL)) .((integer)($arr[$i]===NULL)) .((integer)is_null($arr[$i])) ); var_dump($arr); ?> it gave the following results: 0 : ==NULL 0.0 : ==NULL '0' : nothing worked =) '0.0' : nothing... '' : ==NULL FALSE : ==NULL 'false' : nothing NULL : ==NULL, ===NULL, is_null() 'NULL' : nothing enjoy =) disappear
i wrote a small case study to compare is_null / == NULL / === NULL Here's the code :: <?php $Array = array ( 0 , '' , FALSE , NULL ) ; $ArrayCount = count ( $Array ) ; $String .= '$Array = ' . "array ( 0 , '' , FALSE , NULL ) " ; for ( $i = 0 ; $i < $ArrayCount ; $i++ ) { if ( $Array [ $i ] == NULL ) { $String .= '$Array [ $i ] == NULL :: $Array [ ' . $i . ' ] ' ; } if ( $Array [ $i ] === NULL ) { $String .= '$Array [ $i ] === NULL :: $Array [ ' . $i . ' ] ' ; } if ( is_null ( $Array [ $i ] ) ) { $String .= 'is_null ( $Array [ $i ] ) :: $Array [ ' . $i . ' ] ' ; } } echo $String ; ?> Here's the results i got :: $Array = array ( 0 , '' , FALSE , NULL ) $Array [ $i ] == NULL :: $Array [ 0 ] $Array [ $i ] == NULL :: $Array [ 1 ] $Array [ $i ] == NULL :: $Array [ 2 ] $Array [ $i ] == NULL :: $Array [ 3 ] $Array [ $i ] === NULL :: $Array [ 3 ] is_null ( $Array [ $i ] ) :: $Array [ 3 ] powderz
Hello all. The function is really simple, but it seems like others are making it confusing than it should be. is_null() will return true if: the variable has not been set, or the variable has the following values: NULL or null <?php $var = NULL; $var = null; $var; // or if not declared at all ?> without using is_null(), the proper way to check for null values is using === operator. <?php if ($var === NULL) {} // true if ($var === null) {} // true if (is_null($var)) {} // true /* * Any other values for $var will return false */ ?> With that in mind, you can save an extra function call by avoiding is_null() check and just use <?php if ($var === NULL) {} // or if ($var === null) {} ?> When dealing with database nulls (I can't remember whether it's "null" or "NULL"), you're just going to have to write a wrapper function. Here's my untested function: <?php function isnull($data) { /* * I can't remember the string null value the database returns, so I'm making two assumptions */ if ($data === NULL || $data === 'NULL' || $data === 'null') { return true; } return false; } ?> passion4code
function value_is_null($t){ if (is_array($t)) { return (size_of($t) > 0) } else { return (($t != '') && (strtolower($t) != 'null') && (strlen(trim($t)) > 0)) } } This is fairly generic and will account for a variety of parameters. If you really want to be sure, add a && !is_null($t) inside the last return statement. uioreanu
Don't try to test if ($intSomething==NULL) { ... } use is_null() instead. The first statement misses 0 values. Regards, Calin [Ed. note: this is because == tests for equivalence of value, but not type. NULL evaluates to false, as does 0, so NULL == 0 is true--even though 0 is type int and NULL is type null. You should use either is_null() as noted or ===, which returns true only if its operands are equal and of the same type.] hayley watson
As powderz comments below, "is_null() will return true if the variable has not been set". It will also trigger a notice-level message, like pretty much any use of an unset variable would. Much of the confusion observed comes from users suppressing or otherwise ignoring those messages. Best practice is to ensure that variables are set before using or examining their values - in this case, using isset(). empty() (the other function that tolerates unset variables) would not be suitable, since that doesn't distinguish between an unset variable and one that contains a null. There is a difference between an unset variable and null: a variable that has a null value is an entry in PHP's list of extant variables. An unset variable doesn't appear in the list. thepissboy
After pulling out most of my hair weeding through really unintelligible crap: HERE'S AN EASY WAY TO ELIMINATE NULL VALUES FROM YOUR ARRAYS!! $result = mysql_query("SELECT * FROM someDatabase WHERE page_title != ''",$db); if ($myrow = mysql_fetch_array($result)) { printf("%s\n", $myrow["page_title"]); } else { printf("Scratch head"); } It's all in the SQL Query line. The code says don't bring back anything that's empty!!!! So hopefully, some of us will be able to get back to being in a hurry. powderz
Actually, since a wrapper is going to be written, you can check for your own version null values...if you want to be creative for some reason. <?php function isnull($data) { /** only if you need this if (is_string($data)) { $data = strtolower($data); } */ switch ($data) { // Add whatever your definition of null is // This is just an example //----------------------------- case 'unknown': // continue case 'undefined': // continue //----------------------------- case 'null': // continue case 'NULL': // continue case NULL: return true; } // return false by default return false; } ?> |
Change Languagedebug_zval_dump doubleval empty floatval get_defined_vars get_resource_type gettype import_request_variables intval is_array is_binary is_bool is_buffer is_callable is_double is_float is_int is_integer is_long is_null is_numeric is_object is_real is_resource is_scalar is_string is_unicode isset print_r serialize settype strval unserialize unset var_dump var_export |