Variables, as in any programming language, are a way of saving a particular value at a location. Variables are how you tell the computer that you want to store something in an area of memory.
After you have stored a value in a variable, you can simply reference that variable to retrieve the value which was stored.
In this PHP tutorial, we will look at manipulating variables in PHP and the conventions which are usually followed.
In PHP, variables are defined as follows:
$foo = 'Welcome to Tutorial Arena';
Variables in PHP must begin with a dollar sign ($). This is a common mistake for newcomers to PHP scripting. Variables are also case sensitive. That is:
$foo = 'Welcome to Tutorial Arena'; // is different from $Foo = 'Welcome to Tutorial Arena';
For those who might be familiar with some of the more strict languages, bear in mind that variables do not need to be declared before they are used in PHP. Also, there is only one type of variable and it can store all types of data, whether it be string, integer, float, etc. All variables, no matter what kind of data they will be storing, are specified in the same way.
To make life easier and to make code easier to read, variables are usually named in a standard way. Variables with multiword names are usually named by capitalising the first letter of each word starting with the second word or by using underscores to separate words. Constants, that is, variables that do not change value are usually named in uppercase.
For example:
$myNewVariable = 'Welcome to Tutorial Arena'; $my_new_variable = 'Welcome to Tutorial Arena'; $PI = 3.1415;
There is no rule which specifies how you are to name your variables. You, as the programmer, are free to name them any way that you want. It is a good idea though to name them using the same format that other PHP programmers are using. For one, if your code is any good, then undoubtedly other programmers will scrutinize it, and if you are working on a big project then everybody on the team will need to be following the same conventions.
This tutorial on PHP variables is just to get you started. Feel free to move on to another PHP tutorial in our series.