<span id="Label3"></p><p><p><strong>Global keyword</strong></p></p><p><p>first, An example of using Global:</p></p><p><p>Using Global</p></p><pre class="brush:php;"><pre class="brush:php;"><?php$a = 1; $b = 2;function Sum () { global $a, $b; $b = $a + $b;} Sum (); Echo $b;? ></pre></pre><p><p>The output of the above script will be "3". The global variable $a and $b are declared in the function, and all reference variables of any variable are pointed to the global variable. PHP has no restrictions on the maximum number of global variables a function can Declare.</p></p><p><p>The second way to access variables globally is to customize $GLOBALS arrays with special PHP. The previous example can be written as:</p></p><p><p>Replace Global with $GLOBALS</p></p><pre class="brush:php;"><pre class="brush:php;"><?php$a = 1; $b = 2;function Sum () { </pre></pre><p><p>In $GLOBALS array, each variable is an element, the key name corresponds to the variable name, and the contents of the value Variable. $GLOBALS exists globally because $GLOBALS is a hyper-global variable. The following example shows the usefulness of a hyper-global variable:</p></p><p><p>Examples of Hyper-global variables and scopes are shown</p></p><pre class="brush:php;"><pre class="brush:php;"><?phpfunction Test_global () { ///most of the predefined variables are not "super", they need to use the ' global ' keyword to make them valid in the local area of the Function. Global $HTTP _post_vars; Print $HTTP _post_vars[' name ']; Superglobals are valid in any scope, and they do not require a ' global ' statement. Superglobals was introduced in PHP 4.1.0. Print $_post[' name ';}? ></pre></pre><p><p><strong>Using static variables</strong></p></p><p><p>Another important feature of the variable range is the static variable (variable). A static variable exists only in the local function domain, but its value is not lost when the program executes away from the Scope. Take a look at the following example:</p></p><p><p>Example of a static variable that needs to be shown</p></p><pre class="brush:php;"><pre class="brush:php;"><?phpfunction Test () { $a = 0; Echo $a; $a + +;}? ></pre></pre><p><p>This function is of little use because the value of $a is set to 0 and output "0" each time it is Called. Adding a variable to the $a + + does not work because the variable $a does not exist once you exit the Function. To write a count function that does not lose this count value, define the variable $a as Static:</p></p><p><p>Examples of using static variables</p></p><pre class="brush:php;"><pre class="brush:php;"><?phpfunction Test () { static $a = 0; Echo $a; $a + +;}? ></pre></pre><p><p>now, each call to the Test () function outputs the value of the $a and adds One.</p></p><p><p>Static variables also provide a way to handle recursive functions. A recursive function is a function that calls Itself. Be careful when writing recursive functions, because they can be recursive indefinitely. You must ensure that there is sufficient method to abort the Recursion. This simple function recursively counts to 10, using a static variable $count to determine when to stop:</p></p><p><p>static variables and recursive functions</p></p><pre class="brush:php;"><pre class="brush:php;"><?phpfunction Test () { static $count = 0; $count + +; Echo $count; If ($count <) { Test (); } $count--;}? ></pre></pre><p><p><strong>Note</strong> : Static variables can be declared according to the example Above. If you assign a value to a declaration with the result of an expression, it causes a parse Error.</p></p><p><p>declaring static variables</p></p><pre class="brush:php;"><pre class="brush:php;"><?phpfunction 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;}? ></pre></pre><p><p></p></p><p><p>PHP variables Getting Started tutorial (3) global keyword</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.