Variables are containers to store data. We can add or change or delete data from the variable and use them in our script. Here are some basic rules to create variables.
How to declare variables in PHP with rules and getting type of variables using gettype()
Declaring variables in PHP
Start with $
Next char after $ ( starting ) must be a letter or underscore ( numbers are not allowed )
Only alphabet, number and underscore are allowed ( no space )
PHP variables can take any type of data and change their type within the page also. ( in above example $my_var changes with type of data ) . It depends on its most recent assignment.
Undefined variable
We must declare a variable before using. This code will generate warning.
<?Php
error_reporting(E_ALL);
echo $name1;
?>
Output
PHP Notice: Undefined variable: name1 in D:\php_files\test.php on line 4
Before using the variable, we can check if it is declared or not by using is_set() or by using empty().
<?Php
error_reporting(E_ALL);
if(isset($name1)){
echo $name1;
}else{
echo " Variabe is not declared ";
}
?>
Examples of Local Global & Static variables in PHP & scope inside and outside a functions
Local & Global
The variables declared as global are available throughout the script ( including inside the functions ).
Variables declared inside functions is know as local variable ( to that function ) and are only available inside the function. So local variables declared inside a page are not available inside the functions of the page.
<?Php
$v1=4;
function test(){
$v1=10;
echo "Value inside the function:".$v1;
}
test();
echo "<br>Value outside the function:".$v1;
?>
Output
Value inside the function:10
Value outside the function:4
GLOBALS
The associative array $GLOBALS holds the name of the variables as key and content as value of the variable. Once the variable is declared as element of $GLOBAL variable, then it is available throughout the script (including inside the functions).
$v1=4;
$v2=3;
function test(){
$GLOBALS[v1]=10;
$GLOBALS[v2]=20;
$GLOBALS[v3]=$GLOBALS[v1]+$GLOBALS[v2];
}
test();
echo $v3; // Output is 30
echo $v2; // Output is 20
Local variables get reset once the execution of function is over. We can retain the value and use them for subsequent function calls by declaring them as Static variable.
Output is here. See how static variable value is retained after execution of first call of the function whereas the local variable is destroyed.
Static variable :1, Local Variable :1
Static variable :2, Local Variable :1
Life of a variable
All variables are used within the page or within the script present in a page. As you are reading this page some variables are used to display the content of the page. These variables are lost as soon as this page execution is completed at server and downloaded to your browser. So how to pass the data or retain the data across the pages ?
←Unset variablesPassing variables from one page to other →
Predefined Variables
PHP provides some predefined variable which we can use directly in our script ( without declaring before ).
Superglobals — Built-in variables that are always available in all scopes