处理全局变量

在 PHP 3 和 PHP 4 早些版本中处理全局变量的重心放在了易用上,而现在,焦点放到了如何更安全的处理它们上。在 PHP 3 中下面的代码工作正常,但是 PHP 4 中就要使用 unset($GLOBALS["id"]);。这仅仅是全局变量处理的一个问题。应当总是使用 $GLOBALS,在更新版本的 PHP 4 中应该强迫自己在大部分情况中使用它。更多信息请参考 global 引用一节。

例子 C-1. 全局变量的移植

<?php
$id
= 1;
function
test()
{
    global
$id;
    unset(
$id);
}
test();
echo(
$id); // 在 PHP 4 中这里会打印出 1
?>

add a note add a note User Contributed Notes
MattyC
27-Apr-2003 03: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
26-Mar-2003 06: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
05-Jan-2003 08: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
24-Dec-2002 04: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
01-Nov-2002 11: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.