PHP: Handling of global variables - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Migrating from PHP/FI 2 to PHP 3 Cookies

Last updated: Sat, 27 Jan 2007

view this page in

# Handling of global variables

While handling of global variables had the focus on to be easy in PHP 3 and early versions of PHP 4, the focus has changed to be more secure. While in PHP 3 the following example worked fine, in PHP 4 it has to be unset(`unset($GLOBALS["id"])`);. This is only one issue of global variable handling. You should always have used `$GLOBALS`, with newer versions of PHP 4 you are forced to do so in most cases. Read more on this subject in the `global` references section.

Пример C-1. Migration of global variables

`<?php
$id = 1;
function test()
{
global $id;
unset($id);
}
test();
echo($id); // This will print out 1 in PHP 4
?>`

add a note User Contributed Notes
Handling of global variables
MattyC
26-Apr-2003 12:16
`Payson Welch is correct
I have a language file that is included from a common include file. I have to declare the globals before the include file and then AGAIN in the funtion!!!!
// include language file
global $lDays, $lDay, $lWeeks, $lWeek;
include "/usr/www/users/foundry/Templates/language/$site_language.inc";
function format_length($nDays){
global $lDays, $lDay, $lWeeks, $lWeek;
if($nDays > 0 && $nDays < 7):
if($nDays == 1):
$sDays = "$nDays $lDay";
else:
$sDays = "$nDays $lDays";
endif;
elseif($nDays >= 7):
$w = ceil($nDays / 7);
if($w == 1):
$sDays = number_format($w,0) ." $lWeek";
else:
$sDays= number_format($w,0) ." $lWeeks";
endif;
endif;
return $sDays;
}`
Ming Deng AT OEone
25-Mar-2003 02:49
`Response to Payson Welch's notes:
This should not be a big concern, as long as your variables are used in classes or functions in that included file. PHP is a scriptable language, when you include a php file, that mean you run it. But if contents of the included file are classes or functions, they just get defined.`
Payson Welch
04-Jan-2003 04:16
`Just a quick note on passing and including.
If you have a top level script with several levels of includes you must declare any global variables that you want the included files to be able to see BEFORE your include statements.
global $var;
$var = 5;
include("file.php");
If you try to change the variable AFTER you have included the file it appears the changes will not be reflecated inside of the included file.
(I could be wrong I don't have the most current version of PHP)
-two|face
zulu_gold@hotmail.com`
brooke at jump dot net
23-Dec-2002 12:09
`Better (and likely more correct) way to look at it is this:
In php3, the global statement caused named variables to become aliases. In php4, the global statement constructs references. In php3, there are no references. In php4, references are what get unset, not the pointed-to values. The same change affects function parameters passed by reference. In php3, you got aliases. In php4, you get references. If you unset a reference, then you've lost only the reference. The original variable remains.
function foo(&$bar) {
unset($bar);
$bar = 27;
}
$x = 5;
foo($x);
assert($x == 5);`
jcjollant at hotmail dot com
31-Oct-2002 07:22
`Just to clarify something that confused me at first :
global $var; does not declare $var as global, it should be read as "there is a global variable that's called $var"
Hence, a global variable must be explicitely declared as "global" in ALL functions willing to use it.
I guess I was expecting the opposite for some reasons.`
add a note
Migrating from PHP/FI 2 to PHP 3 Cookies

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST