Re: 'Global' variable scope.
| From: | Zeev Suraski | Date: | Sat, 30 Jun 2001 09:17:41 +0000 |
| Subject: | Re: 'Global' variable scope. | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-58498@lists.php.net to get a copy of this message | ||
That's simply not the way things work. Local variables are local to the context of the function that declared them, and are not supposed to be accessible from any other context, including that of nested function calls (that's how most languages behave).
At 14:01 29/6/2001, John Parker wrote:
Hi, all. I'm just wondering why a variable defined within a function is not available as a global to functions called from the same scope. There's probably a really good reason for this, but I'm not sure I know what it is. Feel free to educate me. Ta muchly, JP. --- Code example follows --- <?PHP // Simple global variable scope test. Function-- Zeev Suraski <zeev@zend.com> CTO & co-founder, Zend Technologies Ltd. http://www.zend.com/startTests() { $G_test = "Hello, World."; globalTest(); } Function globalTest() { global $G_test; if (isset($G_test)) { echo ("Global variable 'test' - value: '<B>".$G_test."</B>'.<P>\n"); } else echo ("Global variable 'test' is not set.<P>\n"); }?>------------------------------------------------------------------John Parker email: jparker@itchannel.netInternet Project Manager/Software Engineer web:www.itchannel.netitCHANNEL International Ltd. tel: +44 (0) 1202 291939-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net