note 12787 modified in language.variables.scope by danbrown

From: Date: Mon, 18 May 2009 01:45:27 +0000
Subject: note 12787 modified in language.variables.scope by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155593@lists.php.net to get a copy of this message
On confusing aspect about global scope... If you want to access a variable such as a cookie inside a function, but theres a chance it may not even be defined, you need to access it using he GLOBALS array, not by defining it as global. This wont work correctly.... <?php function isLoggedin() { global $cookie_username; if (isset($cookie_username) echo "blah.."; } ?> This will.. <?php function isLoggedin() { if (isset($GLOBALS["cookie_username"])) echo "blah.."; } ?> --was-- On confusing aspect about global scope... If you want to access a variable such as a cookie inside a function, but theres a chance it may not even be defined, you need to access it using he GLOBALS array, not by defining it as global. This wont work correctly.... function isLoggedin() { global $cookie_username; if (isset($cookie_username) echo "blah.."; } This will.. function isLoggedin() { if (isset($GLOBALS["cookie_username"])) echo "blah.."; } http://php.net/manual/en/language.variables.scope.php

« previous php.notes (#155593) next »