note 125888 added to language.variables.scope
| From: | () | Date: | Mon, 08 Mar 2021 03:07:53 +0000 |
| Subject: | note 125888 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219328@lists.php.net to get a copy of this message | ||
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();
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.247 (proxied: 200.120.135.218)
----
Manual Page -- http://php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/125888
Del: integrated -- https://master.php.net/note/delete/125888/integrated
Del: useless -- https://master.php.net/note/delete/125888/useless
Del: bad code -- https://master.php.net/note/delete/125888/bad+code
Del: spam -- https://master.php.net/note/delete/125888/spam
Del: non-english -- https://master.php.net/note/delete/125888/non-english
Del: in docs -- https://master.php.net/note/delete/125888/in+docs
Del: other reasons-- https://master.php.net/note/delete/125888
Reject -- https://master.php.net/note/reject/125888
Search -- https://master.php.net/manage/user-notes.php