php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13714 global $foo; initializes $foo partially
Submitted: 2001-10-17 10:41 UTC Modified: 2001-10-17 11:14 UTC
From: sander@php.net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.0.6 OS: Windows 2000
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sander@php.net
New email:
PHP Version: OS:

 

 [2001-10-17 10:41 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-17 10:58 UTC] 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 11:14 UTC] jeroen@php.net
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>
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 09:00:02 2026 UTC