The Concept of Using Variables
With examples, let us find out what variables are, why do we need them and when to use them
In our previous example, we built a program that greets us by our names. That was pretty exciting. But what if we want to change the name of the person to be greeted upon?
One could simply suggest changing the data quoted in (‘) to the name of the person. Sure, that could work if you don’t mind opening the file and making that change every time a new person comes in.
Introducing — the variables
Consider a variable as a container, a storage or simply a box that we use to store some data. The nature of a variable is that we are not sure what is going to be stored inside of it, but certainly, we need a place to capture and store some values and use it when needed.
Like buying a plastic container from a store. We are not sure exactly what we are going to put into it for storage but we know we are going to put something into that container for the purpose to retrieve it later when we need it.
In our case, we don’t know what the name of the new person is going to be, but we do know there is going to be a name and we need a container to store that name, because it changes from time to time and we can retrieve that name from the container when needed.
We don’t really care about when the name was changed in the container, nor do we care how it was changed. We only care about the value of that name at the time we retrieve it and use it in a display command.
In this case, a variable is our perfect choice.
Now we know why we need to use a variable, let’s take a look at how we come about using it.
How to use a variable
Declare the variable:
Before using a variable, we need to first of all, inform the computer that there is a variable available to use. We do that by declaring a variable in the program. There are many different ways to declare a variable among different languages, but they all follow the same concept and the reason for doing so.
When declaring a variable, we need to give that variable a form of identification, so that we can refer to this variable by its identification later when we need it. We do this by assigning a name to a variable.
In our case, we needed a variable to store people’s names, thus we call this variable “names”. We can then declare this variable before using it in PHP as below.
<?php $names;?>
Code Explanation:
The dollar sign ($) at the front of the variable tells the computer to treat the rest of the sentence as a variable. Think of a dollar sign the mark for a computer to differentiate variables and commands.
The word “names” is what we call this variable, an identification or variable name for the computer to refer to.
Of course, semicolon (;) indicates the end of this instruction.
The entire line of code could be translated into: ”hey computer! We are telling you there is a variable available to use, and it’s called names.”
Important: When naming a variable, make sure it is unique within the program. No two variables should have the same name as it will confuse the system on which one to use.
Important: Variable names are also case sensitive on some systems. $names and $Names are not the same variables!
Assign a value to the variable:
Let’s now store something into this variable. How about a name, if that’s not why we needed this variable already.
We call the action of giving a value to a variable “assigning values”. In our case, we are going to assign a value to the variable, like below.
<?php $names; $names='Tom';?>
Code Explanation:
Since “Tom” is data (information or value), we again wrap it with quote (‘) so that computers know to treat them as data but not commands.
We assign the value of a variable by using an equal sign (=). We can think of this symbol as another command in the system. It takes the value of “Tom” and gives it to a variable named “names”. It is very important to know the sequence of this assignment.
Note: It is very important to understand that, $names=’Tom’ is not to be translated to “$name is equivalent to Tom”. This expression does not have its mathematical interpretations.
Computers will first make the value of “Tom” and then assign its value to variable called “names”. NOT the other way around.
I know it is common for us to think and read from the left to right, but in computer’s world, It is totally the opposite. In our assignment example above, the computer makes a value of “Tom” first then give it to a variable.
I know this doesn’t make much sense and seems useless. But believe me, the importance of this understanding will pay off big time in the later chapters.
And of course, once again, semicolon marks the end of the instruction. I promise this will be the last time I explain this.
Use the variable:
The point of having a variable is to use its value when needed. It is quite clear in our scenario regarding why and where we can use the variable.
We choose to adopt the use of a variable because we are not sure what the name would be at the time we display the message. And we use this variable when the program was instructed to display a message. Therefore, the variable is used as below:
<?php $names; $names='Tom'; echo 'Good morning master '.$names;?>
Code Explanation:
Again, echo is the command to instruct the computer to display something.
What quoted between the (‘) sign is the contents of the display action.
Notice we didn’t put the variable “$names” within the quoted sentence. The reason is simple, a variable is not data but a container or storage that stores data. When we retrieve its data, we simply place it at the location where we want it to be placed within an instruction.
We also need to concatenate (glue) the data of the variable with our greeting message together before being provided to the echo command for the system to display as a whole.
We do so, by putting a period (.) between the data and the variable. In PHP, combining data or variables are done through the use of period (.) while other languages such as C or Java may use plus sign (+).
Summary
We use variables:
- When we are not sure what the contents/data is going to be.
- When we want to store some data for later use or to be used on multiple occasions.
- We do not care about when and how the data was changed. We only want to get its value/data when we need them.
- Variables need to be declared before being used.
- Variables are assigned from the right to the left, meaning the value is made before it can be assigned to a variable.
- We combine variable data with other forms of data with concatenation signs such as period (.) or plus sign (+) depending on the language used.
Awesome! I hope you already feel like a programmer by now. You’ve come a long way and I encourage you taking a break, let these knowledge sink in. Try to relate the understanding of variables with real life examples.
We will be further exploring some more tricks when using variables in the next article.
New articles are added every Wednesday. Please stay tuned.