From 7ba426901948f50aed1ca094cd2675bfe877486e Mon Sep 17 00:00:00 2001 From: Philip Olson Date: Fri, 18 Jul 2003 05:43:49 +0000 Subject: [PATCH] Created sections for static and global as requested in bug #16234. Also made informal examples formal. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@135349 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/variables.xml | 97 +++++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 38 deletions(-) diff --git a/language/variables.xml b/language/variables.xml index a52369cd4c..285fc71fa3 100644 --- a/language/variables.xml +++ b/language/variables.xml @@ -1,5 +1,5 @@ - + Variables @@ -362,11 +362,18 @@ Test(); This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in - that function. An example: + that function. - - - + + + The global keyword + + First, an example use of global: + + + + Using global + ]]> - - + + + The above script will output "3". By declaring @@ -399,9 +407,10 @@ echo $b; the special PHP-defined $GLOBALS array. The previous example can be rewritten as: - - - + + + Using <varname>$GLOBALS</varname> instead of global + ]]> - - + + + The $GLOBALS array is an associative array with @@ -429,9 +439,9 @@ echo $b; Here's an example demonstrating the power of superglobals: - - - + + Example demonstrating superglobals and scope + ]]> - + - + + + + Using static variables Another important feature of variable scoping is the static variable. A static variable exists @@ -460,9 +473,10 @@ function test_global() when program execution leaves this scope. Consider the following example: - - - + + + Example demonstrating need for static variables + ]]> - - - + + + This function is quite useless since every time it is called it sets $a to 0 and prints @@ -485,9 +499,10 @@ function Test () counting function which will not lose track of the current count, the $a variable is declared static: - - - + + + Example use of static variables + ]]> - - - + + + Now, every time the Test() function is called it will print the value of $a and increment it. @@ -515,9 +530,10 @@ function Test() simple function recursively counts to 10, using the static variable $count to know when to stop: - - - + + + Static variables with recursive functions + ]]> - - + + + + + + References with global and static variables The Zend Engine 1, driving PHP4, implements the - static and global modifier for - variables in terms of references. For example, a true global variable + static and + global modifier + for variables in terms of + references. For example, a true global variable imported inside a function scope with the global statement actually creates a reference to the global variable. This can lead to unexpected behaviour which the following example addresses: @@ -643,8 +665,7 @@ Static object: object(stdClass)(1) { variable, it's not remembered when you call the &get_instance_ref() function a second time. - - +