PHP variable Scope instance detailed

Source: Internet
Author: User
What is a variable scope?

Variables are used, to conform to the defined rules of the variable. Variables have to be used in a valid range, and if the variable goes beyond a finite range, the variable is meaningless, as if we had 100 dollars, we can buy 100 dollars or less, if the item exceeds 100 yuan, then these 100 pieces will not be used. 100 dollars is the equivalent of this variable, and the range within 100 blocks is equivalent to the scope.

PHP variables are divided into: local variables, global variables, and static variables because of scope, as in the following table:

Scope Description
Local variables A variable defined inside a function, scoped to the scope of the function in which it is located
Global variables Variables outside the defined function are scoped to the entire PHP file, but cannot be used inside the user-defined function. If you want users to use global variables inside user-defined functions, declare global variables using the Global keyword
static variables The ability to retain the value of a variable after the function call has ended, and when it returns to its scope again, it can continue to use the original value. The normal variable is that after the function call, the stored data value is cleared and the memory space occupied is freed. When using a static variable, declare the variable with the keyword static and put the keyword static before the variable you want to define

A variable defined inside a function whose scope is the function in which it is located is considered to be a completely different variable if it is assigned outside the function. When you exit a function that declares a variable, the variable and the corresponding value are cleared.

Instance

This example is used to compare variables (local variables) that are assigned within a function and variables that are assigned outside the function (global variables), as follows:

<?phpheader ("Content-type:text/html;charset=utf-8"); $exam = "defined outside the function";                                     Declaring a global variable function add () {    $exam = "defined within a function";                                Declares the local variable    echo "the output in the function is:". $exam. " <br/> ";         Output local variable}add ();                                                  The call function echo "output outside the function is:". $exam;                    Output global variable?>

The results of the operation are as follows:

Static variables can be used in many places. For example, using static variables in blogs to record the number of visitors, each time a user visits and leaves, can retain the current number of visitors. Static variables can also be used in a chat room to record the user's chat content.

Example

In the following instance, using static variables and normal variables, colleagues output a data to see how the two functions are different, the code is as follows:

<?phpfunction zdy0 () {    static $message = 0;           Initialize static variable    $message +=1;    echo $message. " ";} function zdy1 () {    $message = 0;           Initialize static variable    $message +=1;    echo $message. " ";} for ($i =0; $i <10; $i + +)   zdy0 ();   Output 1~10echo "<br>"; for ($i =0; $i <10; $i + +)   zdy1 ();//output 10 x 1echo "<br>"; >

Code Run Result:

Detailed Examples:

The Custom Function Zdy () is the output from 1~10 a total of 10 digits, and the zdy1 () function outputs 10 1. Because the Custom function Zdy () contains static variable $message, and zdy1 in function $message () is a normal variable. Two variables are initialized to 0, and two functions are invoked using a for loop, and the result function zdy () retains the value in the static variable $message after it is called. The initialization of a static variable is only performed on the first invocation of the function and is no longer initialized. When the function zdy1 () is called, its variable $message loses its original value and is re-initialized to 0.

Global variables can be accessed anywhere in the program, but cannot be used inside a user-defined function. If you want users to use global variables inside user-defined functions, declare global variables using the Global keyword

Example

The following applies a global variable to a custom function and does not apply a global variable for comparison. In this example, two global variables $zy and $zyy are defined, and in the user-defined function LXT (), they are expected to be called in the 5th, 7 rows, and the output of the program is only $zyy

The value "topic.alibabacloud.com" because the Global keyword $ZYY is declared in line 6th. And the 5th line will not have any output, where the $zy and the second row of $zy have no relationship, the instance code is as follows:

<?phpheader ("Content-type:text/html;charset=utf-8"); $zy = "Hello"; $zyy = "topic.alibabacloud.com"; function Lxt () {    echo $zy. " <br> ";  $zy cannot be called, no    global $zyy is exported;       Using the keyword global inside the function, it is not stated that the Echo $zyy is considered a private variable    . " <br> ";  Call $ZYY}LXT ();? >

Code Run Result:

This is the difference between applying global variables and not applying global variables, and in the next section we explain "mutable variables"

Related video tutorial recommended: "Php.cn lonely Nine Cheap (4)-php video Tutorial" Variable scope

Related Article

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.