A variable is a container for storing information: an instance
<? PHP $x=5; $y=6; $z=$x+$y; Echo $z ;? >
Running instances like algebra
X=5y=6z=x+y
In algebra we use letters (such as x) to hold values (such as 5).
From the above expression Z=x+y, we can calculate the value of Z is 11.
In PHP, these three letters are called variables .
Note: Consider variables as containers for storing data. PHP variables
As with algebra, PHP variables can be used to hold values (x=5) and Expressions (z=x+y).
The names of variables can be very short (such as x and y), or they can take more descriptive names (such as Carname, Total_volume). PHP Variable rules:
- The variable starts with the $ sign, followed by the name of the variable
- Variable names must begin with a letter or underscore
- Variable names cannot start with a number
- Variable names can contain only alphanumeric characters and underscores (A-Z, 0-9, and _)
- Variable names are case sensitive ($y and $Y are two different variables)
Note: The PHP variable name is case-sensitive! Creating PHP variables
PHP does not have a command to create a variable.
Variables are created when they are first assigned: instance
<? PHP $txt= "Hello world!" ; $x=5; $y=10.5;? >
Running an instance
After executing the above statement, the variable txt will save the value Hello world!, the variable x will hold the value 5, and the variable y will hold the value 10.5.
Note: If the value you assign to a variable is text, enclose the value in quotation marks. PHP is a loosely-typed language
In the example above, please note that we do not have to tell the data type of the PHP variable.
PHP automatically converts the variable to the correct data type based on its value.
In languages such as C and C + + and Java, a programmer must declare its name and type before using a variable. PHP variable Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable refers to the part of the script that a variable can be referenced/used.
PHP has three different scope of variables:
- Local (partial)
- Global (globally)
- Static (statically)
Local and Global Scopes
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.
The following example tests a variable with local and global scopes: an instance
<?PHP$x= 5;//Global ScopefunctionmyTest () {$y= 10;//Local Scope Echo"Variable:</p> inside the <p> test function"; EchoThe variable x is:$x"; Echo"<br>"; EchoThe variable y is:$x";} MyTest ();Echo"Variable:</p> outside of the <p> test function";EchoThe variable x is:$x";Echo"<br>";EchoThe variable y is:$x";?>
Running an instance
In the example above, there are two variables $x and $y, and a function myTest (). $x is a global variable because it is declared outside of a function, and $y is a local variable because it is declared within a function.
If we output a value of two variables inside the myTest () function, $y will output the locally declared value, but cannot $x value because it is created outside of the function.
Then, if you output a value of two variables outside of the myTest () function, the value of the $x is output, but the $y value is not output because it is a local variable and is created inside MyTest ().
Note: You can create local variables of the same name in different functions, because local variables can only be recognized by the function in which they are created. PHP Global Keywords
The global keyword is used to access variables within the function.
To do this, use the Global keyword before (inside the function) variable: instance
<? PHP $x=5; $y=10; function myTest () { global$x,$y; $y=$x+$y;} MyTest (); Echo $y // Output?>
Running an instance
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.
The above example can be rewritten like this: instance
<? PHP $x=5; $y=10; function myTest () { $GLOBALS[' y ']=$GLOBALS[' x ']+$GLOBALS[' y '];} myTest (); Echo $y // Output?>
Running instance PHP static keyword
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: instance
<? PHP function myTest () { static$x=0; Echo $x ; $x+ +;} MyTest (); MyTest (); MyTest ();? >
Running an instance
Then, whenever the function is called, the information stored by the variable is the information contained in the last Call of the function.
Note: The variable is still a local variable of the function.
"PHP Learning" PHP variable