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:
-
The variable starts with the $ sign, followed by the name of the variable.
-
The variable name must begin with an underscore and a letter.
-
Variable names can contain only alphabetic characters and underscores. (A-Z, 0-9, and _)
-
Variable names are case-sensitive ($S and $s are not the same variable)
PHP variable Scope
-
Local (partial)
-
Global (globally)
-
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