php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #46458 Tricky, undocumented trait of global keyword
Submitted: 2008-11-01 19:24 UTC Modified: 2008-11-04 16:32 UTC
From: gmtfn at yahoo dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Linux
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: gmtfn at yahoo dot com
New email:
PHP Version: OS:

 

 [2008-11-01 19:24 UTC] gmtfn at yahoo dot com
Description:
------------
Qualifying a variable with the keyword global has a tricky but undocumented effect -- it doesn't merely allow code in a function to access global variable, but it actually creates global variables. This should be clearly described in the manual.

Reproduce code:
---------------
function main() {
  global $num;	
  $num = 10;
  test();
}

function test()
{
  $number = $GLOBALS['num'];
  echo "num = $number";
}

Expected result:
----------------
num = 

because $num isn't declared outside all functions

Actual result:
--------------
num = 10

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-04 16:32 UTC] vrana@php.net
Variables in PHP aren't declared so storing a value to a variable creates it.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 01:00:01 2026 UTC