Sunday, April 14, 2013

Variables


All PHP variables begin with a dollar sign '$' followed by the variable name. Variable names must always begin with either a letter or an underscore '_'.
Example:
$variable_name
$variableName
$VAR
$Var
$var1
$_var
All the examples above are valid variable names. PHP variables are case sensitive so $var and $Var are two different variables. 
Now, let us look at some invalid variable names.
$_1var
$1var
$var!^
Variable names cannot begin with a number or an underscore followed by a number. Variable names cannot also contain any character other than numbers (0-9), letters (a-z, A-Z) and underscores(_).


Naming Conventions


There are two main naming conventions used in naming PHP variables. 
First, the Camel case where the second and the succeeding words are started with a capital letter.
$variableName
$thisIsVariable
$awesomeVariableName1

Second, all letters are in small caps and the words are separated by underscores.
$variable_name
$this_is_a_variable
$awesome_variable_name1

It does not really matter what naming convention you use unless you are required to use a particular one. The most important thing to remember is that you must always be consistent in naming your variables. If you use Camel case in a particular project, then use it all through out the project.

Another important thing to remember when naming variables is that you must give meaningful variable names. Variable names are usually made out of nouns. Do not just name your variables $x, $y, $var1, $var2. Make an effort to give them meaningful names because it makes the code easier for you and most importantly, for others to read. Name your variable according to its purpose. If a variable will hold the sum of two numbers, then you can name it $sum;  if a variable will hold a name of a person, then you can name it $personName. I know it is hard at first but as time goes by, it will become natural and you will definitely appreciate it. Therefore, you must practice those things as you start learning PHP.


Variable Declaration and Initialization


When assigning a value to a variable, the assignment operator is used '='. The syntax for assigning a value to a variable is 
variable name = value;
Make sure you put a semi-colon ';' at the end of the line. Every PHP statement must always end in a semi-colon or else, you will get an error.
Here are some examples:
<?php
$var = 1;
$var2 = "hello world";
$price = 3.1;
?>
There are three main data types that you can use in your variables, these are: integer, double and string. If you are confuse on what data types are, do not worry because we will discuss it later in another tutorial.

PHP is a weakly-typed language.This means variables' data type are not predefined so it can contain any type of data. You can change the data type of a variable any time by assigning a value with a different data type. PHP will automatically determine the type of the variable depending on the value assigned to it.
Example:
<?php
$var = 1;
$var = "hello world";
$var = 3.1;
?>
In the first line of code, $var is an integer. In the second line, "hello world" which is a string is assigned to $var. Therefore, its type is now a string. In the last line of code, 3.1, a double, is assigned to $var so its type now is change into a double. 


Printing the Value of a Variable


The syntax for printing the value of a variable is this:
<?php
echo $var;
?>
In the above code, $var was not given a value so the code will not print anything. Let us look at another example.
<?php
$var = "hello!";
echo $var;
?>
In this case, $var was given a value. The output will be:
hello!
Let us now look at the output that our previous example about type juggling will give.
<?php
$var = 1;
$var = "hello world";
$var = 3.1;
echo $var
?>
Output:
3.1
As you can see, the value of a variable is the last one that is assigned to it.