Create (declare) PHP variables
PHP does not have a command to declare variables.
The variable is created when you assign it to it for the first time:
<? $txt= "Hello world!" $x=5$y=10.5echo$txt
In the above statement execution, the variable txt will hold the value Hello world!, and the variable x will hold the value 5.
Note: When you assign a text value to a variable, enclose the text value in quotation marks.
PHP is a weakly typed language
In the above example, we notice that you do not have to declare the data type of the variable to PHP.
PHP automatically converts variables to the correct data type based on the value of the variable.
In a strongly typed programming language, we must declare (define) the type and name of the variable before using it.
PHP variable Scope
The scope of a variable is the part of the script in which variables can be referenced/used.
PHP has four different scope of variables:
- Local (native)
- Global (globally)
- Static (statically)
- Parameter (parametric)
Local and global scopes
Variables defined outside of all functions, with global scope. In addition to functions, global variables can be accessed by any part of the script, to access a global variable in a function, and to use the Global keyword.
The variables declared inside the PHP function are local variables and can only be accessed inside the function:
<?PHP$x= 5;//Global VariablesfunctionmyTest () {$y= 10;//Local Variables Echo"<p> test function inside variable:<p>"; EchoThe variable x is:$x"; Echo"<br>"; Echo"The variable y is:$y"; }
Use function to call mytest (); Echo"<p> test function outside variable:<p>"; EchoThe variable x is:$x"; Echo"<br>"; Echo"The variable y is:$y"; ?>
In the above example, the MyTest () function defines $x and $y variables. $x variable is declared outside the function, so it is a global variable, $y variable is declared inside the function so it is a local variable.
When we call the MyTest () function and output the value of two variables, the function will output the value of the local variable $y, but cannot output the value of the $x because $x variable is defined outside the function and cannot be used within the function, if you want to access a global variable in a function, you need to use the Global keyword.
Then we output the value of two variables outside of the mytest () function, which will output the value of the global part variable $x, but cannot output $y value because $y variable is defined in the function and belongs to the local variable.
Tips: You can use the same variable names in different functions, because the variable names defined within these functions are local variables and only work within that function.
PHP Global keyword (promotion scope)
The global keyword is used within a function to access globally variable.
To invoke a global variable defined outside a function within a function, we need to add the global keyword before the variable in the function:
<? $x=5$y=10function myTest () { global $x,$y; $y=$x+$yecho$y//
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.
The above example can be written like this:
<? $x=5$y=10function myTest () { $GLOBALS [' y ']=$GLOBALS[' x ']+$GLOBALS[' y '];} Echo $y
Static Scope
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.
To do this, use the static keyword When you declare a variable for the first time:
<? PHP function myTest () { static$x=0; Echo $x ; $x+ +;} myTest (); MyTest (); MyTest ();? >
Then, each time the function is called, the variable retains the value of the function before it was called.
Note: The variable is still a local variable of the function.
Parameter scope
A parameter is a local variable that passes a value to a function by calling code.
Parameters are declared in the parameter list as part of the function declaration:
<? PHP function myTest ($x) { echo$x;} MyTest (5);? >
PHP Quick Start Learning-2