锟斤拷锟斤拷锟侥凤拷围锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷谋锟斤拷锟斤拷锟揭诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫э拷锟轿э拷锟斤拷锟斤拷蟛糠值锟� PHP 锟斤拷锟斤拷只锟斤拷一锟斤拷锟斤拷锟斤拷锟侥凤拷围锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷姆锟轿э拷锟斤拷同锟斤拷锟斤拷锟斤拷锟斤拷 include 锟斤拷 require 锟斤拷锟斤拷锟斤拷募锟斤拷锟斤拷锟斤拷纾�
<?php
$a = 1;
include 'b.inc';
?>
锟斤拷锟斤拷锟斤拷锟� $a 锟斤拷锟斤拷锟节帮拷锟斤拷锟侥硷拷 b.inc 锟斤拷锟斤拷效锟斤拷锟斤拷锟角o拷锟斤拷锟矫伙拷锟皆讹拷锟藉函锟斤拷锟叫o拷一锟斤拷锟街诧拷锟斤拷锟斤拷锟斤拷围锟斤拷锟斤拷锟斤拷锟诫。锟轿猴拷锟斤拷锟节猴拷锟斤拷锟节诧拷锟侥憋拷锟斤拷锟斤拷缺省锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷诰植锟斤拷锟斤拷锟斤拷锟轿э拷凇锟斤拷锟斤拷纾�
<?php
$a = 1; /* global scope */
function Test()
{
echo $a; /* reference to local scope variable */
}
Test();
?>
锟斤拷锟斤拷疟锟斤拷锟斤拷锟斤拷锟斤拷魏锟斤拷锟斤拷锟斤拷锟斤拷为 echo 锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷植锟斤拷姹撅拷谋锟斤拷锟� $a锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿э拷冢锟斤拷锟斤拷锟矫伙拷斜锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷注锟解到 PHP 锟斤拷全锟街憋拷锟斤拷锟斤拷 C 锟斤拷锟斤拷锟斤拷一锟斤拷悴煌拷锟斤拷锟� C 锟斤拷锟斤拷锟叫o拷全锟街憋拷锟斤拷锟节猴拷锟斤拷锟斤拷锟皆讹拷锟斤拷效锟斤拷锟斤拷锟角憋拷锟街诧拷锟斤拷锟斤拷锟斤拷锟角★拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭恍╋拷锟斤拷猓拷锟叫╋拷丝锟斤拷懿锟叫★拷木透谋锟斤拷锟揭伙拷锟饺拷直锟斤拷锟斤拷锟絇HP 锟斤拷全锟街憋拷锟斤拷锟节猴拷锟斤拷锟斤拷使锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷为 global锟斤拷
锟斤拷锟饺o拷一锟斤拷使锟斤拷 global 锟斤拷锟斤拷锟接o拷
Example #1 使锟斤拷 global
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
锟斤拷锟较脚憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟�"3"锟斤拷锟节猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷全锟街憋拷锟斤拷 $a 锟斤拷 $b 之锟襟,讹拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫讹拷锟斤拷指锟斤拷锟斤拷全锟街版本锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟杰癸拷锟斤拷锟斤拷锟斤拷全锟街憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷PHP 没锟斤拷锟斤拷锟狡★拷
锟斤拷全锟街凤拷围锟节凤拷锟绞憋拷锟斤拷锟侥第讹拷锟斤拷锟届法锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� PHP 锟皆讹拷锟斤拷 $GLOBALS 锟斤拷锟介。前锟斤拷锟斤拷锟斤拷涌锟斤拷锟叫达拷桑锟�
Example #2 使锟斤拷 $GLOBALS 锟斤拷锟� global
<?php
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
?>
$GLOBALS 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟介,每一锟斤拷锟斤拷锟斤拷为一锟斤拷元锟截o拷锟斤拷锟斤拷锟斤拷应锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷应锟斤拷锟斤拷锟斤拷锟斤拷锟捷★拷$GLOBALS 之锟斤拷锟斤拷锟斤拷全锟街凤拷围锟节达拷锟节o拷锟斤拷锟斤拷为 $GLOBALS 锟斤拷一锟斤拷锟斤拷全锟街憋拷锟斤拷锟斤拷锟斤拷锟铰凤拷锟斤拷锟斤拷示锟剿筹拷全锟街憋拷锟斤拷锟斤拷锟矫达拷锟斤拷
Example #3 锟斤拷示锟斤拷全锟街憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
<?php
function test_global()
{
// 锟斤拷锟斤拷锟斤拷锟皆わ拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 "super"锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷 'global' 锟截硷拷锟斤拷锟斤拷使锟斤拷锟斤拷锟节猴拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷效锟斤拷
global $HTTP_POST_VARS;
echo $HTTP_POST_VARS['name'];
// Superglobals 锟斤拷锟轿何凤拷围锟节讹拷锟斤拷效锟斤拷锟斤拷锟角诧拷锟斤拷锟斤拷要 'global' 锟斤拷锟斤拷锟斤拷Superglobals 锟斤拷锟斤拷 PHP 4.1.0 锟斤拷锟斤拷摹锟�
echo $_POST['name'];
}
?>
锟斤拷锟斤拷锟斤拷围锟斤拷锟斤拷一锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷锟斤拷态锟斤拷锟斤拷锟斤拷static variable锟斤拷锟斤拷锟斤拷态锟斤拷锟斤拷锟斤拷锟节局诧拷锟斤拷锟斤拷锟斤拷锟叫达拷锟节o拷锟斤拷锟斤拷锟斤拷锟斤拷执锟斤拷锟诫开锟斤拷锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷失锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷樱锟�
Example #4 锟斤拷示锟斤拷要锟斤拷态锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
<?php
function Test()
{
$a = 0;
echo $a;
$a++;
}
?>
锟斤拷锟斤拷锟斤拷没什么锟矫达拷锟斤拷锟斤拷为每锟轿碉拷锟斤拷时锟斤拷锟结将 $a 锟斤拷值锟斤拷为 0 锟斤拷锟斤拷锟� 0锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷 $a++ 没锟斤拷锟斤拷锟矫o拷锟斤拷为一锟斤拷锟剿筹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� $a 锟酵诧拷锟斤拷锟斤拷锟剿★拷要写一锟斤拷锟斤拷锟结丢失锟斤拷锟轿硷拷锟斤拷值锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷 $a 锟斤拷锟斤拷为锟斤拷态锟侥o拷
Example #5 使锟矫撅拷态锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
<?php
function test()
{
static $a = 0;
echo $a;
$a++;
}
?>
锟斤拷锟节o拷锟斤拷锟斤拷 $a 锟斤拷锟节碉拷一锟轿碉拷锟斤拷 test() 锟斤拷锟斤拷时锟斤拷锟斤拷始锟斤拷锟斤拷之锟斤拷每锟轿碉拷锟斤拷 test() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� $a 锟斤拷值锟斤拷锟斤拷一锟斤拷
锟斤拷态锟斤拷锟斤拷也锟结供锟斤拷一锟街达拷锟斤拷莨楹拷锟斤拷姆锟斤拷锟斤拷锟斤拷莨楹拷锟斤拷锟揭伙拷值锟斤拷锟斤拷约锟斤拷暮锟斤拷锟斤拷锟叫达拷莨楹拷锟绞币★拷模锟斤拷锟轿拷锟斤拷芑锟斤拷锟斤拷锟捷癸拷锟斤拷去锟斤拷锟斤拷锟斤拷确锟斤拷锟叫筹拷值姆锟斤拷锟斤拷锟斤拷锟街癸拷莨椤o拷锟斤拷锟斤拷锟斤拷锟津单的猴拷锟斤拷锟捷癸拷锟斤拷锟斤拷锟� 10锟斤拷使锟矫撅拷态锟斤拷锟斤拷 $count 锟斤拷锟叫断猴拷时停止锟斤拷
Example #6 锟斤拷态锟斤拷锟斤拷锟斤拷莨楹拷锟�
<?php
function test()
{
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
test();
}
$count--;
}
?>
Note:
锟斤拷态锟斤拷锟斤拷锟斤拷锟皆帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫憋拷锟绞斤拷慕锟斤拷锟斤拷锟戒赋值锟结导锟铰斤拷锟斤拷锟斤拷锟斤拷
Example #7 锟斤拷锟斤拷锟斤拷态锟斤拷锟斤拷
<?php
function foo(){
static $int = 0; // correct
static $int = 1+2; // wrong (as it is an expression)
static $int = sqrt(121); // wrong (as it is an expression too)
$int++;
echo $int;
}
?>
锟斤拷态锟斤拷锟斤拷锟斤拷锟节憋拷锟斤拷时锟斤拷锟斤拷锟侥★拷
Note:
锟节猴拷锟斤拷之锟斤拷使锟斤拷 global 锟截硷拷锟街诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟街拷诎锟斤拷锟斤拷募锟绞憋拷锟�
锟斤拷 Zend 锟斤拷锟斤拷 1 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 PHP4锟斤拷锟斤拷锟节憋拷锟斤拷锟斤拷 static 锟斤拷 global 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥凤拷式实锟街的★拷锟斤拷锟界,锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟节诧拷锟斤拷 global 锟斤拷涞硷拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷全锟街憋拷锟斤拷实锟斤拷锟斤拷锟角斤拷锟斤拷锟斤拷一锟斤拷锟斤拷全锟街憋拷锟斤拷锟斤拷锟斤拷锟矫★拷锟斤拷锟叫匡拷锟杰碉拷锟斤拷预锟斤拷之锟斤拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞撅拷模锟�
<?php
function test_global_ref() {
global $obj;
$obj = &new stdclass;
}
function test_global_noref() {
global $obj;
$obj = new stdclass;
}
test_global_ref();
var_dump($obj);
test_global_noref();
var_dump($obj);
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
锟斤拷锟狡碉拷锟斤拷为也锟斤拷锟斤拷锟斤拷 static 锟斤拷洹o拷锟斤拷貌锟斤拷锟斤拷蔷锟教拷卮娲拷模锟�
<?php
function &get_instance_ref() {
static $obj;
echo 'Static object: ';
var_dump($obj);
if (!isset($obj)) {
// 锟斤拷一锟斤拷锟斤拷锟矫革拷值锟斤拷锟斤拷态锟斤拷锟斤拷
$obj = &new stdclass;
}
$obj->property++;
return $obj;
}
function &get_instance_noref() {
static $obj;
echo 'Static object: ';
var_dump($obj);
if (!isset($obj)) {
// 锟斤拷一锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷态锟斤拷锟斤拷
$obj = new stdclass;
}
$obj->property++;
return $obj;
}
$obj1 = get_instance_ref();
$still_obj1 = get_instance_ref();
echo "\n";
$obj2 = get_instance_noref();
$still_obj2 = get_instance_noref();
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
锟斤拷锟斤拷锟斤拷示锟剿碉拷锟斤拷一锟斤拷锟斤拷锟矫革拷值锟斤拷一锟斤拷锟斤拷态锟斤拷锟斤拷时锟斤拷锟节讹拷锟轿碉拷锟斤拷 &get_instance_ref() 锟斤拷锟斤拷时锟斤拷值锟斤拷没锟叫憋拷锟斤拷住锟斤拷