note 85203 deleted from language.variables.scope by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:45:21 +0000 |
| Subject: | note 85203 deleted from language.variables.scope by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217939@lists.php.net to get a copy of this message | ||
Note Submitter: ddarjany at yahoo dot com
----
Note that if you declare a variable in a function, then set it as global in that function, its value
will not be retained outside of that function. This was tripping me up for a while so I thought it
would be worth noting.
<?PHP
foo();
echo $a; // echoes nothing
bar();
echo $b; //echoes "b";
function foo() {
$a = "a";
global $a;
}
function bar() {
global $b;
$b = "b";
}
?>