PHP global variable _php tutorial

Source: Internet
Author: User
< span="">

< span="">

< span="">
 
    < span=""> < span="">    =< span="">  "  why"< span="">  = "What"  < span=""> 
ChangeName (); < span="">< span=""> . "
"< span="">?>

The result of executing the code is: My name was why, not the ChangeName () after performing the What. Analyze the reason, because the function body changename within the $name variable is set to local variables by default, $name scope is within the changename. So, modify the code and add the global variables as follows:

 
    < span="">< span="">< span="">< span=""> < span="">    =< span="">  "  why"< span="">  = "What"  < span=""> 
ChangeName (); < span="">< span=""> . "
"< span="">?>

Attention:< span="">

The result of defining a global variable is still my name is why, which surprises me. It turns< span="">out, "that is, when a function refers to an external variable, it can be declared within a function through global, so that the variable can be used in the function (equivalent to passing in as a parameter). Then, modify the code again:

 
    < span="">=  < span="">  < span="">  "Why" < span="">  < span="">  < span="">  < span="">   < span="">  = "What  "< span="">  .  < span="">   < span=""> "
"< span="">?>

The results of this operation are: My name is what, description< span="">

 
    < span="">=  < span="">  < span="">  "Why" < span="">  < span="">  < span="">  < span="">  < span=""> = "What < span=""> "  < span="">   < span="">    < span="">  = "  where".< span="">    < span="">  < span=""> "
"< span="">?>

 < span="">

< span="">< span="">

< span="">

 
    < span="">=< span="">"< span="">< span="">Why" [' name '] = "What"  < span=""> < span="">< span="">< span=""> . "
"< span="">?>

  < span="">

< span="">

!--?   = 1
      
        = 2
        
          [' var2 '] = &[' var1 '  < Span echo< span> . "
" = 1 = 2 < Span Test2 () {span> , = & . "
" ?>

The value of the

$GLOBALS is always available in PHP. Another example:

 
    < span="">=< span="">1< span="">([' var1 '< span="">< span="">< span=""> < span="">< span="">< span=""> . "
" < span=""> < span=""> = 1 < span=""> < span=""> < span=""> < span="">< span="">< span="">< span="">(< span="">< < span="">/c40>< span="">?>< span="">< span="">

  

http://www.bkjia.com/PHPjc/749824.html www.bkjia.com true http://www.bkjia.com/PHPjc/749824.html techarticle ? = "why" = "What" changename (); "My name is" ... " br/"? The result of executing the code is: My name was why, not the ChangeName () after performing the What. Analyze the reason, this is because the function body ...

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