Variables are "containers" used to store information:
<?php $x = 5 ; $y = 6 ; $z = $x + $y ; echo $z ; ?>
x=5y=6z=x+y
In algebra, we take a letter (like x) and assign it a value (like 5).
From the above expression z=x+y, we can calculate the value of z to be 11.
In PHP, these letters are called variables .
| Variables are containers used to store data. |
Similar to algebra, PHP variables can be assigned a value (x=5) or an expression (z=x+y).
Variables can have very short names (such as x and y) or more descriptive names (such as age, carname, totalvolume).
PHP variable rules:
Variables start with a $ sign followed by the name of the variable
Variable names must start with a letter or underscore character
Variable names can only contain alphanumeric characters and underscores (Az, 0-9, and _ )
Variable names cannot contain spaces
Variable names are case-sensitive ($y and $Y are two different variables)
| PHP statements and PHP variables are case-sensitive. |
PHP has no command to declare variables.
A variable is created the first time you assign a value to it:
<?php $ txt = " Hello world! " ; $x = 5 ; $y = 10.5 ; ?>
In the execution of the above statement, 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, put quotation marks around the text value.
In the above example, we noticed that it is not necessary to declare the data type of the variable to PHP.
PHP will automatically convert the variable to the correct data type based on its value.
In a strongly typed programming language, we must declare (define) the type and name of the variable before using it.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has four different variable scopes:
local
global
static
parameter
Variables defined outside all functions have 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, you need to use the global keyword.
Variables declared inside a PHP function are local variables and can only be accessed inside the function:
<?php$x = 5 ; // Global variable function myTest (){ $y = 10 ; // Local variable echo "<p>Variable in test function:<p>" ; echo "Variable x is: $x " ; echo "<br>" ; echo "Variable y is: $y " ;} myTest ();echo "<p>Variable outside the test function:<p>" ;echo "Variable x is: $x " ;echo "<br>" ;echo "Variable y is: $y " ; ?>In the above example myTest() function defines $x and $y variables. The $x variable is declared outside the function, so it is a global variable, and the $y variable is declared inside the function, so it is a local variable.
When we call the myTest() function and output the values of two variables, the function will output the value of the local variable $y, but cannot output the value of $x, because the $x variable is defined outside the function and cannot be used within the function. If To access a global variable in a function, use the global keyword.
Then we output the values of the two variables outside the myTest() function. The function will output the value of all local variables $x, but cannot output the value of $y, because the $y variable is defined in the function and is a local variable.
| You can use the same variable name in different functions, because the variable names defined in these functions are local variables and only affect that function. |
The global keyword is used to access global variables within a function.
To call a global variable defined outside the function within a function, we need to add the global keyword before the variable in the function:
<?php $x = 5 ; $y = 10 ; function myTest ( ) { global $x , $y ; $y = $x + $y ; } myTest ( ) ; echo $y ; // Output 15 ?>
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 used directly to update global variables.
The above example can be written like this:
<?php $x = 5 ; $y = 10 ; function myTest ( ) { $GLOBALS [ ' y ' ] = $GLOBALS [ ' x ' ] + $GLOBALS [ ' y ' ] ; } myTest ( ) ; echo $y ; ?>
When a function completes, all of its variables are usually deleted. However, sometimes you want a local variable not to be deleted.
To do this, use the static keyword the first time you declare the variable:
<?php function myTest ( ) { static $x = 0 ; echo $x ; $x ++; } myTest ( ) ; myTest ( ) ; myTest ( ) ; ?>
Then, each time the function is called, the variable will retain the value from the previous time the function was called.
Note: This variable is still local to the function.
Parameters are local variables whose values are passed to the function through the calling code.
Parameters are declared in the parameter list, as part of the function declaration:
<?php function myTest ( $x ) { echo $x ; } myTest ( 5 ) ; ?>
We will discuss this in more detail in the PHP Functions chapter.