note 59782 added to function.is-null

From: Date: Thu, 15 Dec 2005 15:34:07 +0000
Subject: note 59782 added to function.is-null
Groups: php.notes 
Request: Send a blank email to php-notes+get-100201@lists.php.net to get a copy of this message
I have variables that may be set to empty ($test = "";) when passed into a fuction and as such may be set, but not null, but still empty. I wanted to know the difference between these so I created a test displaying all errors. (I hate even seeing notices). first I check isset, is_null, and empty on an unset variable, then set the variable empty ($test = "";) <?php error_reporting(2047); if(is_null($test)) { print 'This is Null<BR>'; } else { print 'Not Null<BR>'; } if(isset($test)) { print 'This is Set<BR>'; } else { print 'This is Not Set<BR>'; } if(empty($test)) { print 'This is Empty<BR>'; } else { print 'This is Not Empty<BR>'; } $test = ''; if(is_null($test)) { print 'This is Null<BR>'; } else { print 'This is Not Null<BR>'; } if(isset($test)) { print 'This is Set<BR>'; } else { print 'This is Not Set<BR>'; } if(empty($test)) { print 'This is Empty<BR>'; } else { print 'This is Not Empty<BR>'; } ?> And the results ---------------------------------------------- Notice: Undefined variable: test in /var/www/htdocs/test.php on line 4 This is Null This is Not Set This is Empty This is Not Null This is Set This is Empty We can conclude that is_null will error on an unset variable, and thus is not the same as isset. ---- Manual Page -- http://www.php.net/manual/en/function.is-null.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59782 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59782&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59782&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59782&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59782&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59782&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59782&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59782&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59782&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100201) next »