note 113115 deleted from language.variables.scope by cmb
| From: | cmb@php.net | Date: | Sun, 27 Oct 2019 08:29:25 +0000 |
| Subject: | note 113115 deleted from language.variables.scope by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212779@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
<?php
// if you really want to create a variable within its own scope
// that does not have access to variables outside its scope create a function
$var = "hello";
$func = function(){
// declare variables here that will only last throughout this scope
if( !isset($var) ) // var will not be set in this scope
{
$var = "i was out of scope";
}
echo $var;
};
echo "$var<br />";
$func(); // invoke the function
echo "<br />".'$var'." never changed from $var";
?>
outputs :
hello
i was out of scope
$var never changed from hello