Php learning: identifying the scope of variables

Source: Internet
Author: User
The existence of a variable has its life cycle. we can make it exist in a small function or the whole program. For variables declared in general, we call them local variables. they can only exist in the current program segment. Instead, use the variable task 2 declared by $ globals to identify the scope of the variables.

⚑Local variables and global variables

The existence of a variable has its life cycle. we can make it exist in a small function or the whole program. Variables declared in general are called local variables and can only exist in the current program segment. variables declared using $ globals are valid throughout the program on the current page.

Example:
The code is as follows:
$ A = 1;
$ B = 2;
Function sum ()
{$;
$ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>

In this program,
In rows 2nd to 3, we create two variables a and B and assign them 1 and 2 respectively.
Rows 3rd to 7th define a self-added function sum (). The function is to add the variables a and B in sum and assign the value to B.
Line 3 calls the sum function.
Line 2: use echo to output the value of B.
Some people may think that the output value on the webpage must be 3 at this time, but after running, you will find that the value is still 2, that is, the original value of B. This is because of local variables. variables declared in rows 2nd to 3 cannot be used in the sum () function, that is, in the sum function, the names of a and B are the same, but there is no relationship between them. Therefore, the value of B in the final output is the value of B in the 3rd rows.

However, if we modify the following style:
The code is as follows:
$ A = 1;
$ B = 2;
Function sum ()
{
Global $ a, $ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>

We found that in the sum function, we added a global modifier to variables a and B. At this time, a and B have a relationship with a and B outside the function, they are the same variables. Therefore, when the program is running, the result is 3. Therefore, when declaring global variables, we only need to add a modifier global to them when using them locally (in this example, in the sum function, they can inherit external values, so they are no longer local variables.

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.