PHP global variable and static variable analysis detailed

Source: Internet
Author: User
Tags php tutorial

. static variables
Static $a = 0 within the function;
Note: Assigning a value to an expression in a declaration can result in parsing errors such as static $a =3+3; (error)
A static variable exists only in a local function field (within a function), the value of the variable is not lost when the function is finished, and can be used to recursively call

In the scope of variables in the PHP tutorial, there is a very important variable-php static variable. In the following article, we will give you a detailed code example of the main usage of static variables of PHP, and I hope to help you.

Take a look at the following example:

Example demonstrates the need for PHP static variable static example

< PHP function Test () {$w 3sky = 0; echo $w 3sky; $w 3sky++}?>

This function is not useful because the value of $w 3sky is set to 0 and output "0" each time it is invoked. Adding a variable to the $w 3sky++ does not work, because once the function is exited, the variable $w 3sky does not exist. To write a count function that does not lose the value of this count, define the variable $w 3sky as static:

Example of using PHP static variable static

< PHP function Test () {static $w 3sky = 0; echo $w 3sky; $w 3sky++}?>

Now, each call to the test () function will output $w 3sky value and add one.

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, as you may be able to recursively go down indefinitely. There is a need to ensure that there are adequate methods to abort recursion. This simple function recursively counts to 10, using static variables $count to determine when to stop

<?php
function Zdy ()
{
static $message = 0; Initialization of a static variable
$message +=1; Static variable plus 1
echo $message. "         "; Output static variables
}
function Zdy1 ()
{
$message = 0; declaring function Internal variables
$message + 1; Local variable plus 1
echo $message. "   "; Output Local Variables
}
For ($i =0 $i <10; $i + +)/output 1 to 10
Zdy ();
echo "<p>";
For ($i =0 $i <10; $i + +)/output 10 1
Zdy1 ();
echo "<br>";
?>

The output is:
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 1 1

Global variables
Global variables defined in a function body can be used outside of the function body, and global variables defined in the function body cannot be used in the function body, and access to variables globally can be in a special PHP custom $globals array:
For example: $globals ["b"] = $globals ["a"] + $globals ["B"];
A real global variable imported in a function field with the global statement actually establishes a reference to a global variable
Global $obj;
Note: The static and global definitions of variables are implemented in an applied manner

6. Assign value to variable: Transfer address Assignment (simple reference):
$bar = & $foo; Add & sign to the variable that will be assigned the value
Changing the new variable will affect the original variable, and this assignment is faster
Note: Only named variables can pass address assignment
Note: if
$bar = & $a;
$bar = & $foo;
Changing the value of $bar can only change the value of variable foo without changing the value of a (reference changed)

$my _var = ' Hello World ';

Test_global ();

function Test_global () {

Now in the local scope

The $my _var variable doesn ' t exist

Produces error: "Undefined Variable:my_var"

echo $my _var;

Now let ' s important the variable

Global $my _var;

Works

echo $my _var;

}

? >

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.