note 93675 deleted from functions.returning-values by danbrown

From: Date: Tue, 22 Sep 2009 15:06:28 +0000
Subject: note 93675 deleted from functions.returning-values by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160940@lists.php.net to get a copy of this message
Note Submitter: pinkgothic at gmail dot com ---- 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.

« previous php.notes (#160940) next »