php--about function (2) variable range

Source: Internet
Author: User

The range of variables in PHP
Local variable: A variable declared in a function is a local variable that can be used only within its own function. A parameter is a local variable that can be called to assign a value.

Global variable: Declared outside the function, after the declaration of the variable, until the end of the entire script can be used, including in the function and {} can be used, in PHP using global variables to the Global keyword to be included in the function in order to Use to, after the global Declaration is the use of globally variable

Static variable variable: A static variable can only be declared in a function (class), cannot be declared globally, use static before a variable  

Function: A variable can be used in multiple invocations of the same function.
1. Static variables are saved in a static code segment
2. A function is shared between multiple invocations, but only declared to memory the first time the function is called, and later invoked, no longer declared, but directly used

local and global variables : example below

<?php
$a = 10; //define a global variable
function Demo1 () {
$a = 5; //Define a local variable
echo "demo1 () =". $a. " <br> ";
}
function Demo2 () {
Global $a; //referencing a global variable
$a +=10;
echo "demo2 () =". $a. " <br> ";
}
Demo1 ();
Demo2 ();
echo "\ $a =". $a; //Note here to escape with \ or output a value of $ A parsed
?>

Output Result:

Demo1 () =5
Demo2 () =20
$a =20

It can be seen from this that: 1, the local general variable is only used inside the function, 2, the function refers to the global variable if the value in the function is changed , then the value of the global variable will also change

Static variables: Examples include the following

Function sta () { //declares an argument with a static variable
static $a = 0;
$a + +;
return $a. "    "; Returns the value of $ A here with return, paying attention to the call statement
}
function Ordi () { //Declare a common function
$a = 0;
$a + +;
echo $a. " ";
}
for ($i =0; $i <10; $i + +) {//Call function, Result: 1 2 3 4 5 6 7 8 9
Echo STA ();
}
echo "<br>";
for ($i =0; $i <10; $i + +) {//Call function, Result: 1 1 1 1 1 1 1 1 1 1
Ordi ();
}

echo "<br>";
Echo STA (); //Call with the static variable function again , the result one by one,
echo "<pre>****************************</pre>";
$str =sta (); The //function str () returns a value, so the function can be assigned to a variable, so a function call is executed at a time, and the result is 12 instead of one, where the value 12 is worth pondering
Echo $str;

The output results are as follows:

1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 1 1
11

****************************

12

Because static variables can only be declared in the function, if we want to use the value of a static variable can only be obtained by calling the function, if the function is called to assign a value to a variable, we may not get the result we want, as in the example above

php--about function (2) variable range

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.