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;
}
Zaobchádzanie s globálnymi premennými
Zatial čo zaobchádzanie s globálnymi premennými malo v PHP 3 a prvých verziách PHP 4 jednoduchý fokus, fokus sa zmenil, aby bol bezpečnejší. Zatial čo následujúci príklad v PHP 3 fungoval v pohode, v PHP 4 to musí byť unset(unset($GLOBALS["id"]));. Toto je len jeden problém zaobchádzania s globálnymi premennými. Mali by ste vždy používať $GLOBALS, pri nových verziách PHP 4 ste tak nútení urobiť vo väčšine prípadov. O tejto téme sa viac dočítate v referenčnej sekcii global.
Example#1 Migrácia globálnych premenných
<?php
$id = 1;
function test()
{
global $id;
unset($id);
}
test();
echo($id); // Toto v PHP 4 vypise 1
?>
Zaobchádzanie s globálnymi premennými
MattyC
26-Apr-2003 12:16
26-Apr-2003 12:16
Ming Deng AT OEone
25-Mar-2003 02:49
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
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
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
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.
