In simple words a variable is anything that can store data or values. In real world; your cupboard, a jar, a piggy bank, are all different types of variables. However; in the world of computers and programming languages, variables must be defined by the user and they can then be used to store values of a particular type.
So, how my computer differentiates between a variable and a normal text? There must be something to tell the computer that this particular entity is a variable. To overcome this hiccup, every variable in PHP starts with a ‘$’ symbol followed by the name of the variable.
$name $whole_sale $WORLDWAR $_rio23
All these above examples are valid PHP variables.
A PHP variable is case sensitive and the name of the variable should not have any space between two strings, although we can use an underscore’_’ symbol.
Further, the name of a variable cannot start with anything other than an alphabet or an underscore ‘_’.
A starting alphabet can be followed by any of the alpha numeric characters and underscore symbol.
Every programming language has its own style of variable declaration, however; the use of variables remains the same. In PHP, declaring a variable is arguably the easiest amongst all the programming languages because of the dynamically typed nature of PHP variables.
What exactly is this dynamically typed nature of PHP variables?
Have you noticed that we have not talked about the data type of a PHP variable anywhere in our variable declaration part discussed above? Well, it’s because PHP is dynamically typed language which automatically does the job of correcting the variable to the data type of the value stored. So as a programmer you do not have to explicitly mention the data type of the variables.
<?php $name= 'MY NAME IS JOHN'; $x=8; $y=14.6; ?>
You can see in the above example that PHP stores a string, an integer and a float value without any data type attached to any of the three variables. Assigning values to a variable for PHP is the same as it was for C or C++. All you need to do is use the assignment operator ‘=’ with the value to be stored in the right side of the operator and the variable on the left side. It is important to note that string data is stored inside single quotes for a PHP variable.
How to use variables in a PHP script?
Now that we have understood the basics of declaring and assigning values to a PHP variable, the next junction is using a PHP variable in the code.
<?php $name = 'Alex'; $age= 21; echo "My name is $name and I am $age years old."; ?>
The above example will display, My name is Alex and I am 21 years old; on the webpage. It is quite clear that we have used double quotations in the echo statement and have directly included the names of the variables in the statement.
<?php $name = 'Alex'; $age= 21; echo 'My name is '.$name.' and I am '.$age.' years old.'; ?>
This PHP script will also display, My name is Alex and I am 21 years old; on the webpage, however; we have used single quotations instead of double quotes in this example. It should be noted that while using the single quotes, the variable name should be concatenated with the statement using the dot (.) symbol and the name of the variable should not be included inside the single quotes.
Apart from simply echoing the values of a variable on the webpage, we can also perform different operations on the PHP variables, just like in other languages such as C or C++. By using operators like (+, -, /, *) we can add, subtract, divide or multiply the values in a variable. It is quite obvious that these operations used directly, are not valid on the string values in a PHP variable.
Thus, PHP variables and its proper use is the basic building block towards the creation of a good website and good knowledge of PHP variables also helps in understanding the more fundamental aspects of PHP programming with ease.
GET YOUR FREE PHP EBOOK!