Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Appendices : Migrating from PHP 3 to PHP 4 : Handling of global variables

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.

Example E.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
?>


Code Examples / Notes » migration4.variables

ming deng

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.


mattyc

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;
 }


jcjollant

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.


payson welch

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

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);


Change Language


Follow Navioo On Twitter
What has changed in PHP 4
Running PHP 3 and PHP 4 concurrently
Migrating Configuration Files
Parser behavior
Error reporting
Initializers
empty('0')
Missing functions
PHP 3 extension
Variable substitution in strings
Cookies
Handling of global variables
eXTReMe Tracker