Bug #13714 Updated: global $foo; initializes $foo partially

From: Date: Wed, 17 Oct 2001 15:14:04 +0000
Subject: Bug #13714 Updated: global $foo; initializes $foo partially
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-68137@lists.php.net to get a copy of this message
ID: 13714 Updated by: jeroen Reported By: sander@php.net Old Status: Open Status: Bogus Bug Type: Scripting Engine problem Operating System: Windows 2000 PHP Version: 4.0.6 New Comment: global $var; will create that variable. It will assign null to $var. It has done so for a very long time, and I think it's correct: If you do a 'global', you indicate that it's a variable, just like var $var; will initialize a object-variable. And thus, the warning (useful for detecting typo's) isn't issued, because it _is_ an existing variable. Because you don't supply a value, null will be used. isset will just indicate wether a variable has a _nonnull-value_, the name is actually a bit confusing. If differs from !is_null() in that no warning will be issued when a variable really doesn't exist. So, you have: - nonexistent var: <warning on use> <isset=no> - variable with value NULL <no warning on use> <isset=no> - variable with othr value <no warning on use> <isset=yes> Previous Comments: ------------------------------------------------------------------------ [2001-10-17 10:58:17] sander@php.net Small addition: <?php error_reporting(E_ALL); function bar() { echo $foo; // prints a warning global $foo; echo $foo; // no warning } bar(); ?> ------------------------------------------------------------------------ [2001-10-17 10:41:10] sander@php.net Consider the following snippet: <?php error_reporting(E_ALL); function bar() { global $foo; } echo $foo; // prints a warning bar(); echo $foo; // no warning ?> The first echo $foo prints a warning saying "Undefined variable: $foo in ..." The second echo $foo doesn't output a warning. The following snippet is even more strange: <?php error_reporting(E_ALL); function bar() { global $foo; return isset($foo)?1:0; } echo isset($foo)?1:0; // prints 0 (correct) echo $foo; // prints a warning (correct) echo bar(); // prints 0 (correct) echo isset($foo)?1:0; // prints 0 (correct) echo $foo; // doesn't print a warning (not correct) ?> So, isset() says $foo doesn't exists (and so does is_null()). If it doesn't exist, echo'ing $foo should produce an error with E_ALL, but it doesn't. I don't know whether this is a bug or undocumented behaviour. If it's not a bug, it's a very strange and confusing behaviour. Tested with 4.0.6, 4.0.7RC3, and 4.2.0-dev from 200110160600, on Windows 2000. ------------------------------------------------------------------------ Edit this bug report at http://bugs.php.net/?id=13714&edit=1

« previous php.dev (#68137) next »