In this tutorial we are going to learn about PHP variables and its types. PHP variables are unique in many aspects that we shall find out in this tutorial.
PHP variables :
Variables are like vessels that carry information within a program. Variables are like containers, that contains the information.
To use the variables initially we need to declare the variable.
PHP declare variable :
Every variable in PHP is denoted by a $ sign.
<?php
$apples="Hi, I am an Apple and a fruit too.";
$zebra="Hi, I am a zebra and an animal too.";
echo $apples;
echo "<br>";
echo $zebra;
?>
output:
Hi, I am an Apple and a fruit too.
Hi, I am a zebra and an animal too.
The words apples and zebras are variable names. The PHP parser usually expects a value from variables, or may use them to store the values from dynamic forms.
Characteristics of PHP variables :
- They start with the $ symbol then the variable name.
- The names of variables must begin with underscore or letter.
- Only numbers, alphabets and the underscore can be part of the PHP variables.
- PHP variables are case sensitive.
Types of PHP Variables :
Output variables :
The echo or print statements are used for displaying information on the screen. Variables may you may declare a variable within a code.
<?php
$a="they are animals.";
$_1="zebra";
$_2="lion";
echo "This is a $_1 and $_2 , and $a";
?>
output:
This is a zebra , lion and they are animals.
You may also use concatenation to declare variables in between two quotes.
<?php
$a="they are animals.";
$_1="zebra";
$_2="lion";
echo "This is a ".$_1."!";
?>
The above code will result as:
This is a zebra!
PHP variables scope :
The variables scope for PHP include
- local,
- global,
- and static.
They can be declared anywhere within the php script.
PHP Global variables :
A variable whose declaration is out of a function is termed as being global, and may only be used outside that particular function(s).
<?php
//Global Variables
$a="December";
function fest(){
//Any global used outsode of this function cannot be called inside the function
echo "<p> Current Month is $a</p>";
}
fest();
echo "<p>Christmas comes in $a</p>";
?>
output:
Current month is
Christmas comes in December
On the above example current month is empty. Since we can’t access the global variables from php function.
PHP Local variables :
PHP local variables can only be used within its area of definition. If defined within a function, it can only be used within that specific function.
Example : When we take the same definition of $a into the function, here is what happens.
<?php
//Local Variables
function fest() {
$a = "December";
//Any local variable used inside of this function cannot be called from outside the function
echo "<p>Current Month is $a</p>";
}
fest();
echo "<p>Christmas comes in $a</p>";
?>
output:
Current Month is December
Christmas comes in
However, whenever there is need to use the global variable within a function, one needs to use the keyword ‘global’.
We shall use the same example as above :
<?php
$a = "December";
function fest() {
global $a;
// We are calling the global variables into this function
echo "<p>Current Month is $a</p>";
}
fest();
echo "<p>Christmas comes in $a</p>";
?>
output:
Current Month is December
Christmas comes in December
PHP $GLOBALS :
PHP variables also allow you to store global variables within arrays.
The syntax is $GLOBALS[index] where the index signifies the name of the variable.
<?php
//Global Variable declared
$a = "December";
echo "<p>Christmas comes in $a</p>";
function fest() {
$GLOBALS["a"] = "January";
//Using the array $GLOBALS[index] to update a global variable locally
}
fest();
echo "<p>New Year comes in $a</p>";
?>
output:
Christmas comes in December
New Year comes in January
The output before the global redefinition within the function has the text “December”. However, after redefinition, the same variable stores the word “January”.
Local variables lose their values after completion of a function. However, this value may still be useful in another function or globally. The Keyword “static” is used.
PHP Static variables:
Static variables are the variables which are used when we want local variables value to be saved for further use, so that whenever the value is called it will display the last value of the variable.
<?php
//PHP static variables
function digits(){
static $b=0;
echo $b;
$b++;
echo ",";
$c=12;
echo $c;
}
digits();
echo "<br>";
digits();
echo "<br>";
digits();
echo $c;?>
output:
0,12
1,12
2,12
The final value is stored in the static $b variable so that it increments at every loop.
Happy Learning 🙂