php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61651 Inexplicable double result with global var in a function
Submitted: 2012-04-06 13:47 UTC Modified: 2012-04-11 19:30 UTC
From: valerio dot bozzolan at gmail dot com Assigned:
Status: Not a bug Package: Website problem
PHP Version: 5.3.10 OS: Linux
Private report: No CVE-ID: None
 [2012-04-06 13:47 UTC] valerio dot bozzolan at gmail dot com
Description:
------------
I already know that nobody in the world will give me an answer, because in a clean file this code works but in the queue of another file it does not work, even if the code of the queue of the second file (which does not work) is exactly identical to what I ran in the first clean file.

Var "$a", in the function of my first file, is normal out of the function, and normal in the function.

Same code in the queue of my second file give me a different result: "$a" is normal out of the funcion, but in the function IS NULL!

(Excuse my English)

Test script:
---------------
unset($a);
$a = "PIPPO";
function foo()
{
	global $a;
	echo "In function: ";
	var_dump($a);
}
echo "Out function: ";
var_dump($a);
echo "<br />";
foo();

Expected result:
----------------
First clean file result:
Out function: string(5) "PIPPO"
In function: string(5) "PIPPO" 

Second file result:
Out function: string(5) "PIPPO"
In function: string(5) "PIPPO"

Actual result:
--------------
First clean file result:
Out function: string(5) "PIPPO"
In function: string(5) "PIPPO" 

Second file result (inexplicable):
Out function: string(5) "PIPPO"
In function: NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-06 14:06 UTC] nikic@php.net
I'm not sure whether or not I got you right: Are you saying that you have two files, with exactly the same content, but they are behaving differently?
 [2012-04-07 15:03 UTC] laruence@php.net
I can not reproduce it.....
 [2012-04-10 10:57 UTC] bjori@php.net
-Status: Open +Status: Not a bug
 [2012-04-10 10:57 UTC] bjori@php.net
This happens because your second file is included inside an function.
Which means your $a in the second file is not the global $a.

When you then enter the function, it pulls in the actual global $a.
 [2012-04-11 19:30 UTC] valerio dot bozzolan at gmail dot com
Sorry... 

Thank you bjori@php.net, it was just like you said:

This happens because your second file is included inside an function.
Which means your $a in the second file is not the global $a.

Another sorry...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 01:01:31 2024 UTC