Go to the menu - Go to the contents

What is a variable?


Previous Chapter Contents Next Chapter

According to the meaning of the word, you should suspect it is something which changes all the time. Indeed the main feature of a variable is to be able to change its value. But strictly speaking, what does it mean?

A variable is a small piece of information stored in the memory temporarily. It has a short life cycle. In PHP a variable exists during the generation process of the page. Once the page has been generated, all variables are removed from the memory because they are of no use anymore. A variable is therefore not a file stored on the hard drive but a temporary piece of informaton.
Creating a variable is up to you, that is, you create it when you think it is needed.
What you should know is that a variable is made up of 2 things:
  • Its name : in order to identify your variable you must name it. For instance "age_of_visitor"
  • Its value : the value is the information conveyed. It can change. For example "17 years".

In this example, I used a variable called "age_of_visitor" with the value "17". The value of a variable can be changed at will and used for any calculations. And when we need it, we call it (by its name) and it tells us what it's set to. ;) It goes on like this: - Hey ! you age_of_visitor variable, what are you set at?
- 17.
- Thanks !
Variables may seem confusing for now but they will be mandatory for your PHP site. For instance, storing a username ("m@teo21") in a name_of_visitor variable, you will be able to use this information to display a personal welcome message like, " Hi m@teo21! welcome". You remember how to display a text in PHP, don't you? ;) The "echo" function you learned in the previous chapter will be very useful for practice!

Previous Chapter Contents Next Chapter