note 106759 added to language.references.pass

From: Date: Mon, 05 Dec 2011 03:18:15 +0000
Subject: note 106759 added to language.references.pass
Groups: php.notes 
Request: Send a blank email to php-notes+get-184895@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 87.212.16.222 ---- Manual Page -- http://www.php.net/manual/en/language.references.pass.php Edit -- https://master.php.net/note/edit/106759 Del: integrated -- https://master.php.net/note/delete/106759/integrated Del: useless -- https://master.php.net/note/delete/106759/useless Del: bad code -- https://master.php.net/note/delete/106759/bad+code Del: spam -- https://master.php.net/note/delete/106759/spam Del: non-english -- https://master.php.net/note/delete/106759/non-english Del: in docs -- https://master.php.net/note/delete/106759/in+docs Del: other reasons-- https://master.php.net/note/delete/106759 Reject -- https://master.php.net/note/reject/106759 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184895) next »