PHP Learning Log-Variable scope

Source: Internet
Author: User

1. Accessing global variables within the global function

<? PHP $x=5; $y=10; function myTest () {global$x,$y; $y=$x+$y;} MyTest (); Echo $y // Output?>

If global is not used in the MyTest function, it cannot be used

PHP stores all global variables in an array called $GLOBALS [index]. Index holds the name of the variable. This array can be accessed inside the function, or it can be used to update global variables directly. such as $globals[' y ']= $GLOBALS [' x ']+ $GLOBALS [' Y ']

2, Static when a function is complete, all its variables are usually deleted. However, there are times when you want a local variable not to be deleted.

function MyTest () {static $x =0;echo $x; $x + +;} MyTest (); MyTest (); myTest (); 012

Each time the function is called, the variable retains the value of the function before it was called. The variable is still a local variable of the function.

PHP Learning Log-Variable scope

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.