Differences between local variables and global variables in php functions

Source: Internet
Author: User
Variables defined in a program are called local variables. they are only used within the program and disappear as the program ends. Variables defined outside the program are called global variables, which can be called by the program but will not disappear as the program ends. Variables defined in a program are called Local variableIs used only within the program and disappears as the program ends. Variables defined outside the program are called Global variables, Can be called by the program, but will not disappear as the program ends. The previous sections introduce the local variables of the custom functions of php functions and the global variables of the custom functions. Define a function. all the variables in this function are local variables. they can only be used within this function. return is the return value of this function. when this function is called, the value can be the variable in the function. that is, local variables. A global variable is a variable defined outside the function. its scope ranges from the definition to the end of the file.

In php, functions are treated as separate program slices, so local variables overwrite global variables, even if the local variables do not define variables with the same global variables, they are overwritten.

The following uses an instance to describe their differences:

 "; Echo" function out: $ s1 "; // output global variable: out s1 // static variable test function count1 () {$ num = 0; $ num ++; echo $ num. "";} function count2 () {static $ num = 0; $ num ++; echo $ num. "" ;}for ($ I = 0; $ I <10; $ I ++) {count1 (); // 11 1 1 1 1 1 1 1 1 1 1} echo"
"; For ($ I = 0; $ I <10; $ I ++) {count2 (); // 1 2 3 4 5 6 7 8 9 10} echo"
"; // Use global variables in functions, and add global $ a =" php "; $ B =" java "; function show () {echo $; // no output global $ B; echo $ B; // defines global and outputs java} show ();?>

Differences between local variables and global variables:

1. local variables can access internal variables within the function and output results; variables inside the function cannot be accessed outside the function, so results cannot be output; if the variable value needs to be called outside the function, the return command must be used to return the value to the main program block for subsequent processing.

2. in PHP, because a function can be considered as a separate program code segment, local variables overwrite the visibility of global variables. Therefore, global variables cannot be called directly in functions. To use a global variable in a function, you must use a keyword to define the target variable. the keyword of this global variable is "global ", with the keyword "global", you can import global data to a local range of a function to tell the function subject that this variable is a global variable.

3. Another important feature of the range of variables in PHP is static variables ). Static variables exist only in the local function domain and are initialized only once. when the execution of a program leaves this scope, the value of the static variables will not disappear and the result of the last execution will be used. In the above example, we use static and do not use static variables. you can make a closer comparison.

[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

The above is a detailed description of the differences between local variables and global variables in php functions. For more information, 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.