Variables in PHP

Getting Started with PHP
Getting Started with PHP : Home
Hello World
Data Types in PHP
Variables in PHP
Operators in PHP
Decision Making in PHP
Iterations in PHP
Functions in PHP
Arrays in PHP
Strings in PHP
Object Oriented PHP
Classes and Objects
Object Interfaces
Static and Final
Advanced Features in PHP
Session in PHP
Cookies in PHP
Server Variables

Variables in PHP
Variables in PHP start with $ followed by name and is case sensitive. The variable name can start with letter or underscore and the rest may be letters, numbers or underscores ($this is a special variable and can't be assigned).


     $variable_1 = 24; // Valid variable
     $_variable = 'loreum'; //valid variable starts with underscore

     $3variable = 2.4; // Not a valid variable starts with number
    // To get the code running you have to remove the above 
    // variable as it will give a warning
     echo "$variable1, $_variable";


Assigning varibaes
Value of one varible can be assigned to another variable. But default varibles are assigned by value. You can assign varaible by reference by simply adding & before the variable which is assigned to the destination variable.
Assign by Value and Assign by Reference
The difference between the assign by value and assign by refence is that the value of the destination varible does not alter when the value of original variable is changed in assign by value. But in case of assign by reference the value of the destination variable changes if any changes are made of original varible.

	$original = 'original data';
	$assign_by_value = $original; // Assigned by value
	$assign_by_ref = &$original;  // assigned by reference

	$original = 'data changed';
	echo "$assign_by_value, $assign_by_ref";
	/*Assign by value will print the value which original 
	variable was holding while assigned and assign by value will 
	print the current value */


Variable Scope
Variable scope in php is only limited by user defined functions. A variable is available only within the context it is defined.
A variable defined outside function can be used anywhere outside the function and same for variable defined inside function.

	$variable_outside = 'hello';
	function test(){
		$variable_inside = 'I am locked in test()';
		echo "$variable_inside<br>";
		echo "$variable_outside<br>"; // Will give an error as this variable
		                           // is not available inside function
	echo "$variable_outside";
	echo "$variable_inside"; // Again this will give error

To run this code you will have to remove the lines which will cause error(i.e. 6 & 11). The error is thrown because $variable_outside is not recognized inside function test() similarly $variable_inside will not be recognized outside function.
Variable scope also extends to included and required files. Thus, User defined functions are the only thing that seprates variable scope.
Predefined Variables
PHP provides many predefined variables which are very helpful. Some of the most used of these variables are following:

We will discuss these predefies variables in more details in later text. So if you are having a little problems, Don't worry we got all covered for you.
Variable Variable
You can have the name of variable also a variable. This is really an amusing feature provided in PHP. You will understand better with an example:

	$var = "ab";
	$$var = "b";
	echo "$var,";
	echo $$var; /*Be cautious Variable variable can not
	be printed inside " " */
	echo "";

While naming your variables try to give them meaningful names otherwise it will create a lot of confusion in large code. Also have set of rules, which lessen the need to look back for variable names.

Login to Track Your Progress