PHP Task Learning 2: Recognize the scope of the variable _php tutorial

Source: Internet
Author: User
local variables and global variables

The existence of a variable has its life cycle, and we can make it exist within a small function, or it can exist throughout the program. For a variable declared in the general case, we call it a local variable, only exists in the current program segment, and the variable declared with $globals will be valid throughout the current page of the program.

Cases:

What Copy to Clipboard refers to:[www.bkjia.com]

 
 
  $a = 1;
$b = 2;
function sum ()
{$a;
$b;
$b = $a + $b;
}
SUM ();
echo$b;
?>

In this procedure,

In line 2nd to 3rd, we set up two variables A and B and assign them values of 1 and 2, respectively.

Line 3rd to 7th, we define a self-added function sum (), whose purpose is to add the variables a and b inside the sum, and assign the added value to B.

Line 8th, call the SUM function.

Line 9th, use echo to output the value of B.

Some people may think that the value of the output on the Web page must be 3, but after running you will find that the value is still 2, that is, the original value of B. This is the cause of the local variables, the variables declared in the 2nd to 3rd line cannot be used in the sum () function, that is, the A and B and 2nd to 3rd rows used in the SUM function are only the same names, but there is no relationship between the two. So, in the final output, B is the value of line 3rd B.

But if we change the program to the following style:

Copy to Clipboard refers to the content: Span style= "color: #f7f7f7; Font-weight:100 ">[www.bkjia.com]

!--? php
$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 the variables A and B, and at this point A and B are related to A and B outside the function. They are the same variable. Therefore, when the program is running, the result is 3. Therefore, when we declare global variables, we only need to use them locally (in this case, in the function sum) and give them a modifier, global, which can inherit the external value and no longer be a local variable.

http://www.bkjia.com/phpjc/364493.html www.bkjia.com true http://www.bkjia.com/phpjc/364493.html techarticle

  • 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.