note 106759 deleted from language.references.pass by tularis

From: Date: Mon, 05 Dec 2011 18:25:09 +0000
Subject: note 106759 deleted from language.references.pass by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184912@lists.php.net to get a copy of this message
Note Submitter: Wander Reason: bad code ---- Sometimes you might want to check if a reference is set or not, when it's optional For instance, you have a function where lots of stuff happens and it returns whether or not the stuff succeeded, and a log can optionally be saved in a reference <?php //Wrong function foo($command,&$log=NULL){ if(isset($log))$enableLogging = true; else $enableLogging = false; /*Do all kind of stuff with $command*/ return $success; } //Will still save a log in $log, even though it is not set foo('things'); //Possibly wrong function foo($command,&$log=NULL){ if($log===NULL)$enableLogging = false; else $enableLogging = true; /*Do all kind of stuff with $command*/ return $success; } //Will NOT save a log, cause $someLog doesn't exist (so $log will be NULL), but it will not throw an error foo('things',$someLog); //WILL save a log, cause $someLog is now set $someLog = ''; foo('things',$someLog); //Probably right function foo($command,&$log=false){ if($log===false)$enableLogging = false; else $enableLogging = true; /*Do all kind of stuff with $command*/ return $success; } //WILL save a log, whether $someLog already exists or not doesn't matter (as long as $someLog hasn't been set to false) foo('things',$someLog); ?>

« previous php.notes (#184912) next »