What is a local variable? Explanation of the range of php variables

Source: Internet
Author: User
The range of a variable is its effective range. Most PHP variables only have a single scope of use, and include files introduced by include and require. Php variable range

The range of a variable is its effective range. Most PHP variables only have a single scope of use, and include files introduced by include and require. When a variable is assigned a value, it may vary with the declared regional location. The previous section describes php variable functions and function references and disreferences. generally, variables are divided into local variables and global variables based on their declared positions. In this section, we will first introduce local variables.

What local variables?

A local variable is also called an internal variable. it is a variable declared inside the function. its scope is limited to the internal function. it is invalid to use this variable after leaving the function, an error occurs during program execution. Not only is the declared variable in the function a local variable, but the parameter set for declaring the function can only be used within the function, so it is also a local variable. The difference is that the specific value of a function parameter is obtained from outside the function (the value is input when the function is called), and the variable can only be assigned a value within the function directly declared in the function. However, their scopes are limited to the internal scope of the function, because each time the function is called, the internal variables of the function will be declared. after the execution, the internal variables of the function will be released.

The following code is used as an example:

 ';} // Call the num function and assign the value to the num (6) parameter; // use two variables outside the function to illegally access echo "function external: $ one + $ two = ". ($ one + $ two);?>

After the above program is executed, the following results are output:

Internal Function: 6 + 2 = 8 // internal variables can be accessed within the function, and external output result function: + = 0 // Two internal variables cannot be accessed outside the function, therefore, results cannot be output.

In the above example, a num () function is declared. when the num () function is called, two variables $ one and $ twe are declared, both of which are local variables. The variable $ one is declared in the parameter and assigned a value during the call. The other variable $ two is declared in the function and assigned a value directly, these two local variables can only be used inside the function and the calculation result is output. When the execution of the num () function ends, these two variables are released. Therefore, when the two variables are accessed outside the function, there is no output result. If the variable value needs to be called outside the functionReturnThe command returns the value to the main program block for subsequent processing.

The code is as follows:

 

After the above program is executed, the following results are output:

Function external use: 8 // Obtain the internal execution result of the function, which is used outside the function.

The above is an example of local variables in the scope of php variables. the next chapter will introduce global variables in the scope of php. please pay attention to the following content.

[Recommended tutorials]

1. php1.cn Dugu jiujian (4)-php video tutorial

2. php programming from getting started to mastering a full set of video tutorials

3. php video tutorial

What is the local variable above? For more details about the scope of php variables, see other related articles in the first PHP community!

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.