PHP ʖ²ἯTH º󍋼/A Ղ 8. ±䁿 ±䁿·¶Χ ?php $a = 1; include "b.inc";? ?php $a = 1; /* global scope */ function Test() {echo $a; /* reference to local scope variable */} Test();? ?php $a = 1; $b = 2; function Sum() {global $a, $b; $b = $a + $b;} Sum(); echo $b;? ?php $a = 1; $b = 2; function Sum() {$GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];} Sum(); echo $b;? Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example: ?php function Test () {$a = 0; echo $a; $a++;}? This function is quite useless since every time it is called it sets $a to 0 and prints "0". The $a++ which increments the variable serves no purpose since as soon as the function exits the $a variable disappears. To make a useful counting function which will not lose track of the current count, the $a variable is declared static: ?php function Test() {static $a = 0; echo $a; $a++;}? Now, every time the Test() function is called it will print the value of $a and increment it. ?php function Test() {static $count = 0; $count++; echo $count; if ($count 10) {Test ();} $count--;}? ?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);? NULL object(stdClass)(0) {} ?php function get_instance_ref() {static $obj; echo "Static object: "; var_dump($obj); if (!isset($obj)) {// Assign a reference to the static variable $obj = new stdclass;} $obj- property++; return $obj;} function get_instance_noref() {static $obj; echo "Static object: "; var_dump($obj); if (!isset($obj)) {// Assign the object to the static variable $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();? Static object: NULL Static object: NULL Static object: NULL Static object: object(stdClass)(1) {["property"]= int(1)} º󍋼/A ưµ㼯A Ԥ¶¨ҥ±䁿 ɏһ¼¶ ¿ɱ䱤