Variable in PHP

Variable is nothing it is just name of the memory location. A Variable is simply a container that is used to store both numeric and non-numeric information.

Use of Variables

Variables help separate data from the program algorithms. The same algorithm can be used for different input data values. For example, suppose that you are developing a calculator program that adds up two numbers, you can create two variables that accept the numbers then you use the variables names in the expression that does the addition.

Rules for Variable declaration

  • A variable starts with the $ sign, followed by the name of the variable.
  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
  • Variable names are case-sensitive ($age and $AGE are two different variables)

Assigning Values to Variables

Assigning a value to a variable in PHP is quite east: use the equality(=) symbol, which also to the PHP's assignment operators.

This assign value on the right side of the equation to the variable on the left. A variable is created the moment you assign a value to it:

<?php
 	$myCar = "Honda";
 	echo $myCar;
?>

In the above example Create a variable $mycar containing a string with value="Honda". To print the car name pass $mycar inside echo statement.


Destroying PHP Variables

To destroy a variable, pass the variable to PHP's unset( ) function. as in the following example:

<?php
	$name="steve";
	echo $name;
	//unset() function destroy the variable reference.
	unset($name);
?>

In the above example declare variable $name hold value="steve". In this program we used unset() function to delete a particular variable. first it show the output: "steve", because we pass unset function after echo statement.

Now pass variable name inside unset($name) function output will show an Notice error(Variable is undefined).


Inspecting Variable Contents(Variable Property)

PHP offers the var_dump( ) function, which accepts a variable and X-rays it for you. Here's an example

<?php
	//define variables
	$name = "Fiona";
	$age=25;
	//display variable contents
	var_dump ($name);
	var_dump($age);
?>

Output string 'Fiona' (length=5) int 25

In the above example We use var_dump() function to check the Contents(property) of variable, $name hold a string value ="Fiona" while $age hold an integer value = 25.

Now pass this variable inside var_dump($name, $age) function . It show all information related this(data type of variable, length(only string), value)


Variable Scopes

Scope of a variable is defined as its extent in program within which it can be accessed, i.e. the scope of a variable is the portion of the program with in which it is visible or can be accessed.

Depending on the scopes, PHP has three variable scopes:

  1. Local variables
  2. Global variables
  3. Static variable

1. Local variables

The variables declared within a function are called local variables to that function and has its scope only in that particular function. In simple words it cannot be accessed outside that function.

Any declaration of a variable outside the function with same name as that of the one within the function is a complete different variable.

Example

<?php
	$num = 60;
	function local_var(){
 		$num = 50;
 		echo "local num = $num \n";
	}
	local_var();
	echo "Variable num outside local_var() is $num \n";
?>

Output

local num = 50
Variable num outside local_var() is 60

2. Global variables

The variables declared outside a function are called global variables. These variables can be accessed directly outside a function.

To get access within a function we need to use the “global” keyword before the variable to refer to the global variable.

Example

<?php
	$num = 20;
	function global_var(){
 		global $num;
 		echo "Variable num inside function : $num \n";
	}
	global_var();
	echo "Variable num outside function : $num \n";
?>

Output

Variable num inside function : 20
Variable num outside function : 20

3. Static variable

It is the characteristic of PHP to delete the variable, ones it completes its execution and the memory is freed. But sometimes we need to store the variables even after the completion of function execution.

To do this we use static keyword and the variables are then called as static variables.

Example

<?php
	function static_var(){
 		static $num = 5;
 		$sum = 2;
 		$sum++;
 		$num++;
 		echo $num "\n";
 		echo $sum "\n";
	}
	static_var();
	static_var();
?>

Output

6 3
7 3