note 93675 added to functions.returning-values
| From: | pinkgothic at gmail dot com | Date: | Tue, 22 Sep 2009 15:05:00 +0000 |
| Subject: | note 93675 added to functions.returning-values | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160939@lists.php.net to get a copy of this message | ||
rusty at socrates dot berkeley dot edu mentions variables getting unset when a function returns
nothing. They're not unset in the strictest sense - they're assigned null.
<?php
function hasNoReturnValue() {
//return null;
}
function hasNoReturnValueButReturns() {
return/*null*/;
}
function returnsNull() {
return null;
}
var_dump(hasNoReturnValue());
var_dump(hasNoReturnValueButReturns());
var_dump(returnsNull());
?>
All of those return NULL.
isset() treats NULL-set Variables as unset, but there is a difference. While for most cases
differentiation isn't necessary, it's worth noting that if you have a construct like
<?php $arr[$key] = null; ?>
Then
<?php array_key_exists($key,$arr); ?>
will evaluate as true, while
<?php isset($arr[$key]); ?>
will not.
Unset variables var_dump as null, too; but only properly unset variables will ever throw an error
relating to an unset variable - NULL-set variables will not.
That doesn't invalidate the approach, mind you. isset() doesn't know the difference, so if
that's the behaviour you want, go for it.
----
Server IP: 217.160.72.57
Probable Submitter: 80.153.202.193
----
Manual Page -- http://www.php.net/manual/en/functions.returning-values.php
Edit -- https://master.php.net/note/edit/93675
Del: integrated -- https://master.php.net/note/delete/93675/integrated
Del: useless -- https://master.php.net/note/delete/93675/useless
Del: bad code -- https://master.php.net/note/delete/93675/bad+code
Del: spam -- https://master.php.net/note/delete/93675/spam
Del: non-english -- https://master.php.net/note/delete/93675/non-english
Del: in docs -- https://master.php.net/note/delete/93675/in+docs
Del: other reasons-- https://master.php.net/note/delete/93675
Reject -- https://master.php.net/note/reject/93675
Search -- https://master.php.net/manage/user-notes.php