Variable & its Scope in PHP

In the last tutorial I have guided you through the Installation Procedure of XAMPP, Now we will discuss variable and its scope. But before that, if you have not seen the previous posts Introduction to PHP & How to install PHP.

What is Variable?

In mathematical terminology, a variable is an entity that can vary or have different values in a different state. In the contrast of variable with the mathematical world, a variable is an entity that could change and store some identifiers and constants. So it is more like a container.

Creating / Declaring PHP Variables:

If you are from C or Java background. Whenever you want to use a variable you need to declare it first then only you can use it to store value, but, Since PHP is an interpreted language, so like other interpreted language, the Variable doesn’t need to declare first. Doesn’t it make sense to call variable a container, as in real world, a container can store anything that is capable to be fit inside it, similarly a PHP variable can store anything. A variable in PHP starts with the $ symbol. For Example:

<?php
$name=”Tekraze “;
$Roll_no=27;
$fees=500.80;
echo “These are my Variable: “;
echo $name, $Roll_no,” “, $fees;
?>

OUTPUT: These are my Variable: Tekraze, 27, 500.80

As shown in the above snippet, the three variable created above $name, $Roll_no, $fees will hold the value Tekraze, 27, and 500.80 correspondingly.