note 80409 added to function.is-null

From: Date: Mon, 14 Jan 2008 20:17:03 +0000
Subject: note 80409 added to function.is-null
Groups: php.notes 
Request: Send a blank email to php-notes+get-135099@lists.php.net to get a copy of this message
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: <?php $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. ---- Server IP: 217.13.201.10 Probable Submitter: 87.234.52.205 ---- Manual Page -- http://www.php.net/manual/en/function.is-null.php Edit -- https://master.php.net/note/edit/80409 Del: integrated -- https://master.php.net/note/delete/80409/integrated Del: useless -- https://master.php.net/note/delete/80409/useless Del: bad code -- https://master.php.net/note/delete/80409/bad+code Del: spam -- https://master.php.net/note/delete/80409/spam Del: non-english -- https://master.php.net/note/delete/80409/non-english Del: in docs -- https://master.php.net/note/delete/80409/in+docs Del: other reasons-- https://master.php.net/note/delete/80409 Reject -- https://master.php.net/note/reject/80409 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135099) next »