PHP Basic 02: Variables

Source: Internet
Author: User

1. Create a variable
<? PHP     $num 1 =;     $num 2 = 15.5;     Echo $num 1+$num 2;     Echo "<br>";? >
2.Local and Global Scopes
<?PHP$x= 5;//Global    functionmyTest () {$y= 10;//Local        Echo"X:$x"; Echo"<br>"; Echo"Y:$y"; Echo"<br>";    } myTest (); Echo"; Echo"X:$x"; Echo"<br>"; Echo"Y:$y";?>
3.PHP Global Keywords
<? PHP     $c = ten;     $d =;     function Test () {        global$c$d$f;         $f $c $d ;    }    Test ();     Echo "$f";? >
4.PHP also stores all global variables in an array named $GLOBALS [index]. The subscript contains the variable name. This array is also accessible within the function and can be used to update global variables directly.
<? PHP     $x = 5;     $y =;     function test4 () {        $GLOBALS$GLOBALS$GLOBALS[' x '];    }    Test4 ();     Echo "$y";? >
5.PHP Static Keywords

Typically, all variables are deleted when the function finishes/executes. However, sometimes I need to not delete a local variable. Achieving this requires a bit of further work. To do this, use the static keyword when you first declare a variable:

<? PHP     function myTest () {          static$x=0;           Echo $x ;           $x+ +;    }    MyTest ();    MyTest ();    MyTest ();? >

PHP Basic 02: 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.