note 125888 deleted from language.variables.scope by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:37:06 +0000 |
| Subject: | note 125888 deleted from language.variables.scope by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217926@lists.php.net to get a copy of this message | ||
Note Submitter: atesin () 6m4i1 ! c0m
----
i hadn't found info on this so i had to make some tests...
inside a function, when globalling a variable that doesn't exist in the parent scope, it will
set to null... i mean...
<?php
function f()
{
global $g;
var_dump($g); // prints 'NULL' with no log message, means if $g is unset globally then
here IS SET to NULL (*)
var_dump($l); // prints 'NULL' and logs the 'undefined variable l' notice in
this line
// (*) notice until the variable $g IS SET to null here, isset($g) will still return false
// because for isset() the var has to be set and not null (see doc for details)...
// maybe the only way to find if a variable is actually unset is throught the log warnings
}
var_dump($g); // prints 'NULL' and logs the 'undefined variable g' in this line
var_dump($g); // same as above but in this line, means the variable is still unset
f();
?>
.
knowing this could be useful in a case like mine... consider something like this...
<?php
//--- account.php ---
$user = 'john doe';
//--- main.php ---
function welcome()
{
global $user;
echo "welcome $user"; // if $user is not set in parent scope it just prints
"welcome "
// the right way afaik
if ( isset($user) )
echo "welcome $user";
}
if ( login_ok() )
incude 'account.php';
welcome();
?>