Re: namespace trouble
| From: | Keith Roberts | Date: | Thu, 31 May 2007 12:47:51 +0000 |
| Subject: | Re: namespace trouble | ||
| References: | 1 2 3 4 | Groups: | php.install |
| Request: | Send a blank email to php-install+get-18031@lists.php.net to get a copy of this message | ||
Hi Pascal. You seem to have a problem with the scope of your variables.
On Thu, 31 May 2007, Pascal Desroche wrote:
To: Andy Stratton <astratton@compassinfo.net> From: Pascal Desroche <pascal@ker.org> Subject: Re: [PHP-INSTALL] namespace trouble Yes Anddy, i did this to get every kind of info i could. i just don't understand why this<?php //here you are declaring $example in the global scope. $example = "php variable test"; PrintExample(); function PrintExample(){ // now you are trying to access a global variable // inside a function echo "$example"; } ?>
Notice: Undefined variable exampleto do this you need to use the global keyword inside the function, like this: function PrintExample() { // tell php you want to access a global variable global $example; // or global $var1, $var2, ... etc echo $example; } It's not good practice to access global variables like that anyway. When you get onto OOP PHP then that would be frowned upon. Usually you would want to pass the global variable as a parameter to the function, like this: PrintExample('testing...'); function PrintExample($example) { echo $example; } HTH Keith ------------------------------------------------------------ http://www.karsites.net http://www.raised-from-the-dead.org.uk This email address is challenge-response protected with http://www.tmda.net ------------------------------------------------------------