Variable is a symbol or name that stands for a value. Variables are used for storing values such as numeric values, characters, character strings, or memory addresses so that they can be used in any part of the program.

Declaring PHP variables

All variables in PHP start with a $ (dollar) sign followed by the name of the variable.

A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores.

If a variable name is more than one word, it can be separated with underscore (for example $employee_code instead of $employeecode).

'$' is a special variable that can not be assigned.

Pictorial presentation of PHP variable naming

php variable

Example : Valid and invalid PHP variables  

  1. <?php  
  2. $abc = 'Welcome';  //valid  
  3. $Abc = ''//valid  
  4. $9xyz = 'Hello world';  //invalid; starts with a number  
  5. $_xyz = 'Hello world';  //valid; starts with an underscore  
  6. $_9xyz = 'Hello world';  //valid  
  7. $aäa = 'Hello world';  //valid; 'ä' is (Extended) ASCII 228.  
  8. ?>  

PHP variable name is case-sensitive

Consider the following example :

  1. <?php  
  2. $abc = 'Welcome';  
  3. echo "Value of abc : $abc";  
  4. echo "Value of ABC : $ABC";  
  5. ?>  

Output of the above example

Value of abc : Welcome
Value of ABC :

In the above example the different capitalization schemes make for different variables.

PHP is a loosely type language

In language such as C, C++ and Java the programmer must declare the name and type of the variable before use it. In PHP the type of the variable does not need to be declared before use it, because types are associated with values rather than variables. As a result a variable can change the type of its value as much as we want.

As previously mentioned you don't need to declare variables or their type before using them in PHP. In the following example none of the variables are declared before they are used, fact is $height is floating number and $width is an integer. 

  1. <?php  
  2. $height = 3.5;  
  3. $width = 4;  
  4. $area=$height*$width;  
  5. echo "Area of the rectangle is : $area";  
  6. ?>  

PHP variables : Assigning by Reference

PHP (from PHP4) offers another way to assign values to variables: assign by reference. This means that the new variable simply points the original variable. Changes to the new variable affect the original, and vice a versa.

Consider the following example :

  1. <?php   
  2. $foo='bob';  
  3. $bar=&$foo;  
  4. $bar="my $bar";  
  5. echo $bar;  
  6. echo '<br />';  
  7. echo $foo;  
  8. ?>  


my bob
my bob

PHP variable variables

You know how to declare variables in PHP. But what if you want the name your variable is a variable itself? In PHP, you have Variable Variables, so you may assign a variable to another variable.

In the following example at line no. 2, we declared a variable called $v which stores the value 'var1' and in line no. 4, "var1" is used as the name of a variable by using two dollar signs. i.e. $$v.

Therefore there are two variables now. $v which stores the value "var1" where as $$v which stores the value var2. At this point $$v and $var1 are equal, both store the value "var2".


  1. <?php  
  2. $v='var1';  
  3. echo $v// prints var1  
  4. $$v = 'var2';   
  5. echo $$v// prints var2  
  6. echo $var1// prints var2  
  7. ?>  

PHP Variables Scope

In PHP, variables can be declared anywhere in the script. We declare the variables for a particular scope. There are two types of scope, the local scope where variables are created and accessed inside a function and global scope where variables are created and accessed outside a function.

Example :

  1. <?php  
  2. //global scope  
  3. $x = 10;  
  4. function var_scope()  
  5. {  
  6. //local scope  
  7. $y=20;  
  8. echo "The value of x is :  $x "."<br />";  
  9. echo "The value of y is :  $y"."<br />";  
  10. }  
  11. var_scope();  
  12. echo "The value of x is :  $x"."<br />";  
  13. echo "The value of y is :  $y ";  
  14. ?>   

In the above script there are two variables $x and $y and a function var_scope(). $x is a global variable since it is declared outside the function and $y is a local variable as it is created inside the function var_scope(). At the end of the script var_scope() function is called, followed by two echo statements. Lets see the output of the script :

The value of x is :
The value of y is : 20
The value of x is : 10
The value of y is :

There are two echo statements inside var_scope() function. It prints the value of $y as it is the locally declared and can not prints the value of $x since it is created outside the function.

The next statement of the script prints the value of $x since it is global variable i.e. not created inside any function.

The last echo statement can not prints the value of $y since it is local variable and it is created inside the function var_scope() function.


The global keyword

We have already learned variables declared outside a function are global. They can be accessed any where in the program except within a function.

To use these variables inside a function the variables must be declared global in that function. To do this we use the global keyword before the variables. Consider the following the example :

Example :

  1. <?php  
  2. $x=2;  
  3. $y=4;  
  4. $z=5;  
  5. $xyz=0;  
  6. function multiple()  
  7. {  
  8. global $x$y$z$xyz;  
  9. $xyz=$x*$y*$z;  
  10. }  
  11. multiple();  
  12. echo $xyz;  
  13. ?>  

In the above example $x, $y, $z, $xyz have initialized with 2, 4, 5, 0. Inside the multiple() function we declared $x, $y, $z, $xyz as global. Therefore all reference of each variable will refer to global version. Now call multiple() anywhere in the script and the variable $xyz will print 40 as it is already referred as global.


PHP static variables

Normally when a function terminates, all of its variables loose its values. Sometimes we want  to  hold these values for further job. Generally those variables which holds the values are called static variables inside a function. To do this we must write the keyword "static" in front of  those  variables. Consider the following example without static variable. 

Example : without static variable

  1. <?php  
  2. function test_variable()  
  3. {  
  4. $x=1;  
  5. echo $x;  
  6. $x++;  
  7. }  
  8. test_variable();  
  9. echo "<br>";  
  10. test_variable();  
  11. echo "<br>";  
  12. test_variable();  
  13. ?>  

In the above script the function test_count() is useless as the last statement $x = $x +1 can not increase the value of $x since every time it is called $x sets to 1 and print 1.




To overcome the problem of the above script $x can be declared as static. See the following example.

Example : with static variable

  1. <?php  
  2. function test_count()  
  3. {  
  4. static $x=1;  
  5. echo $x;  
  6. $x++;  
  7. }  
  8. test_count();  
  9. echo "<br>";  
  10. test_count();  
  11. echo "<br>";  
  12. test_count();  
  13. ?>  

Output :



PHP : Reserve Words

The words in the following table are reserve words and cannot be used as constants, class names, function or method names. You can use them as variable names, but do not use as variable names to avoid confusion.

PHP Keywords

abstract (as of PHP 5) and array() as break
case catch (as of PHP 5) cfunction ( PHP 4 only) class clone (as of PHP 5)
const continue declare default do
else elseif enddeclare endfor endforeach
endif endswitch endwhile extends final (as of PHP 5)
for foreach function global goto (as of PHP 5.3)
if implements (as of PHP 5) interface (as of PHP 5) instanceof (as of PHP 5) private (as of PHP 5)
namespace (as of PHP 5.3) new old_function (PHP 4 only) or throw (as of PHP 5)
protected (as of PHP 5) public (as of PHP 5) static switch xor
try (as of PHP 5) use var while  

Compile-time constants

__CLASS__ _DIR__ (as of PHP 5.3) _FILE_ __LINE__ __FUNCTION__
__METHOD__ __NAMESPACE__ (as of PHP 5.3)      

Language constructs

die() echo() empty() exit() eval()
include() include_once() isset() list() require()
require_once() return() print() unset()