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

search for in the

Přechod z PHP/FI 2 na PHP 3> <Cookies
Last updated: Sat, 24 Mar 2007

view this page in

Obsluha globálních proměnných

Zatímco v PHP 3 a prvních verzích PHP 4 se při obsluze globálních proměnných dbalo především na jednoduchost, nyní se zaměření změnilo směrem k vyšší bezpečnosti. Takže jestliže následující případ dobře fungoval v PHP 3, v PHP 4 je třeba provést unset($GLOBALS["id"]);. Toto je jen jeden aspekt obsluhy globálních proměnných. Měli byste vždy používat $GLOBALS, v novějších verzích PHP 4 budete nuceni tak učinit ve většině případů. Více o této problematice najdete v části global (globální) reference.

Příklad D.1. Změny u globálních proměnných

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



Přechod z PHP/FI 2 na PHP 3> <Cookies
Last updated: Sat, 24 Mar 2007
 
add a note add a note User Contributed Notes
Obsluha globálních proměnných
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.

Přechod z PHP/FI 2 na PHP 3> <Cookies
Last updated: Sat, 24 Mar 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites