What is the difference between a PHP static global variable and a normal global variable?

Source: Internet
Author: User
What is the difference between a PHP static global variable and a normal global variable?
for ($i =1; $i <5; $i + +) {
$glo = 1;
static $glo = 1;
$glo + +;
}
Echo $glo;
?>
In the above example, $glo is a global scope, and the output is different from the non-static keyword, it turns out that the use of static global variables can be used, but the PHP manual only talks about using static variables in functions, as well as static member properties and methods of classes. Does not involve the concept of static global variables, the data found on the network are basically c,c++,java to the static global variable interpretation, very little involved in PHP. I don't know if the static variables in these languages are consistent with PHP.
I now know that both global variables and static variables are placed in the same area of memory. Who else to explain, if PHP has the concept of static global variables, then it is different from ordinary global variables?

------Solution--------------------
The difference is right here.

static $glo = 10;
echo $glo;//1
for ($i =1; $i <5; $i + +) {
Echo $glo;
static $glo = 1;
$glo + +;
}
echo $glo;//5
  • 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.