Search notes:

PHP code snippets: null, empty …

Comparing values to NULL

<!DOCTYPE html>
<html>
<head><title>PHP: null type / NULL value</title></head>
<body>

  <?php function tf($val) { if ($val) { print('true'); } else { print('false'); } } ?>

  <table summary='comparing NULLs'>
    <tr><td> 0    ==  NULL       </td><td><?php tf( 0    ==  NULL       );  ?></td></tr> <!-- true    -->
    <tr><td> 0    === NULL       </td><td><?php tf( 0    === NULL       );  ?></td></tr> <!-- false   -->
    <tr><td> 42   ==  NULL       </td><td><?php tf( 42   ==  NULL       );  ?></td></tr> <!-- false   -->
    <tr><td> 42   === NULL       </td><td><?php tf( 42   === NULL       );  ?></td></tr> <!-- false   -->
    <tr><td> NULL ==  NULL       </td><td><?php tf( NULL ==  NULL       );  ?></td></tr> <!-- true    -->
    <tr><td> NULL === NULL       </td><td><?php tf( NULL === NULL       );  ?></td></tr> <!-- true    -->
    <tr><td> NULL ==  $undef_var </td><td><?php tf( NULL ==  $undef_var );  ?></td></tr> <!-- true    -->
    <tr><td> NULL === $undef_var </td><td><?php tf( NULL === $undef_var );  ?></td></tr> <!-- true    -->
  </table>

</body>
</html>
Github repository about-php, path: /null.html

The empty() function

This example is similar to the one above, but uses the empty() function rather than a comparison with NULL:
<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>empty</title>
</head>
<body>

  <?php


     $var_foo   =  "foo";
     $var_2     =     2 ;
     $var_      =    "" ;
     $var_0     =     0 ;
     $var___    =  "  " ;
     $var_null  =  null ;


     if ( empty ($var_foo  ) ) print "var_foo  is empty<br>"; else print "var_foo  is <b>not</b> empty<br>"; // var_foo  is not empty
     if ( empty ($var_2    ) ) print "var_2    is empty<br>"; else print "var_2    is <b>not</b> empty<br>"; // var_2    is not empty
     if ( empty ($var_     ) ) print "var_     is empty<br>"; else print "var_     is <b>not</b> empty<br>"; // var_     is     empty
     if ( empty ($var_0    ) ) print "var_0    is empty<br>"; else print "var_0    is <b>not</b> empty<br>"; // var_0    is     empty
     if ( empty ($var___   ) ) print "var___   is empty<br>"; else print "var___   is <b>not</b> empty<br>"; // var___   is not empty
     if ( empty ($var_null ) ) print "var_null is empty<br>"; else print "var_null is <b>not</b> empty<br>"; // var_null is     empty
     if ( empty ($var_na   ) ) print "var_na   is empty<br>"; else print "var_na   is <b>not</b> empty<br>"; // var_na   is     empty

     unset ($var_2);

     if ( empty ($var_2    ) ) print "var_2    is empty<br>"; else print "var_2    is <b>not</b> empty<br>"; // var_2    is not empty

  ?>

  <p>Compare with <a href='isset.html'>isset.html</a>

</body>
</html>
Github repository about-php, path: /empty.html
Compare with isset()

See also

Other PHP snippets.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1745910559, '18.188.181.58', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/web/php/snippets/null-empty(109): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78