(c) PHP Learning notes ———— basic syntax: variables

Source: Internet
Author: User


Creating variables

PHP does not have a command to create a variable, it is created when the first assignment is made, and is automatically converted to the correct data type.


PHP Naming conventions:

    1. The variable starts with the $ sign, followed by the name of the variable.

    2. The variable name must begin with an underscore and a letter.

    3. Variable names can contain only alphabetic characters and underscores. (A-Z, 0-9, and _)

    4. Variable names are case-sensitive ($S and $s are not the same variable)


PHP variable Scope

    1. Local (partial)

    2. Global (globally)

    3. Static (statically)


LOCAL Scope and global scope

Variables declared outside the function have Global scope and can only be accessed outside of the function.

Variables declared inside a function have a local scope and can only be accessed inside the function.


Give one of the two examples to improve your understanding.


<?php        $a =1;  //global scope, i.e. A is a goddess         function test () {            $b =2;// Local scope, B is loser           echo  $a;     //now the function inside the output variable A is equal to loser want to find goddess, but Goddess and you are not a world, so value output is empty, dream it! loser                                   echo   "<br>";//Line break            echo  $b;     //value of 2--There is no change, or a person what to do.            echo  "<br>";                   }          &nbSp;    test ();       echo   $a;   // Value is 1, unchanged---time flow, the goddess was Ko Fu handsome abandoned or a person        echo  "<br>";        echo   $b;   //the variable b in the output function outside the function, the output is empty--the goddess was abandoned and returned to find loser, Loser said when I was stupid, the baby in the belly is not mine, so the output is empty.        echo  "<br>";        echo  "The line above is empty! "?>

Global keywords

In the case of the above example, it is to make loser (local variable B) The Magic Weapon of the goddess (global variable a)--to become a high-rich handsome

Used both to access global variables within a function

<?php $a = 1;             Global variable function test () {global $b;//define variable B as a global variable---loser into a high-handsome $b = 2;       } test ();  echo $a;       The value is 1 echo "<br>";  Echo $b; A value of 2--output variable b outside of the function is not empty--both now Gaofu and goddess of how to blind, how blind to engage in?>

Static keyword

In general, all local variables are deleted when the function is finished---both high and handsome, the goddess will lose

However, sometimes I need not delete a local variable----also do not rule out happy life together

<?php function Test () {static $b =0;//defines variable B as a static variable, echo $b;       $b + +; } test ();//value is 0 test ();//value is 1 Test ();//value is 2?>

You can get rid of the static to realize its role ...


This article is from the "Thales" blog, make sure to keep this source http://thales.blog.51cto.com/7311175/1632401

(c) PHP Learning notes ———— basic syntax: 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.