A variable is used to store information and it is accessible later in the program.
It has a user-defined name.
In PHP you don’t need to specify the Datatype of a variable it automatically converts its type according to the assigned value.
There are different types of variables are available –
- Local variable
- Global variable
- Static variable
Contents
1. Variable creation rules
- A variable starts with the
$
sign, followed by the name of the variable. - A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alphanumeric, character, and underscores ( A-Z,0-9, and _ ).
- Variable names are case sensitive ( $x and $X ) are two different variables.
2. Create a variable
A variable is declared using $
following name of the variable and no need to define the datatype.
Syntax –Â
$variablename;
Value is assigned with =
operator.
Syntax –
$variablename = value;
Example –Â
<?php $num; $num = 45; $text = "Hello World"; echo "num : ".$num; echo "<br>text : ".$text;
Output
num : 45 text : Hello World
3. Local variable
This type of variable scope is only within the block. They are not accessible outside of a block.
In the below example, the variable gets destroyed on the function exit.
$num1, $num2, and $sum are not accessible outside the addOne(). If you try to access it then the page displays an undefined variable warning message.
<?php function addOne(){ $num1 = 5; // Local variable $num2 = 10;Â Â // Local variable $sum = $num1 + num3; // Local variable echo 'sum : '.$sum; }
4. Global variable
This type of variable scope is within and outside of the block.
Normally if some variable is defined inside the function then its value is not accessible outside of it.
Example
In the example, try to access the $sum value. When the program is run then it will show  Notice: Undefined variable: sum
.
<?php function sum(){ $num1 = 10; $num2 = 5; $sum; $sum = $num1 + $num2; } sum(); echo 'sum : '.$sum;
In this type of case, the global variable is used to increase the scope of variable usage.
Define global variable
A global variable is defined by adding global
keyword at the front of a variable.
Syntax –
global $variablename;
Example
<?php function sum(){ $num1 = 10; // Local variable $num2 = 5; // Local variable global $sum; // Global variable $sum = $num1 + $num2; } sum = 5; sum(); echo 'sum : '.$sum; sum = 3; sum(); echo "<br>sum : ".$sum;
Output
sum : 15 sum : 18
5. Static variable
A static variable is a variable that doesn’t lose its value on function exit.
Syntax –
static $variablename = value;
Example
<?php function addOne(){ static $num = 0; $num++; echo 'num : '.$num.'<br>'; } addOne(); addOne(); addOne();
Output
num : 1 num : 2 num : 3
6. Conclusion
Use the variables to store data if you want to use it later in the program. You don’t need to define the data type of a variable because it is auto-initialized according to the assigned value.
Only use the global variable when require otherwise avoid it and use the local variable or pass the variable as the parameter in the function.
If you found this tutorial helpful then don't forget to share.