New way to learn PHP

PHP Variable

Variables in PHP

Variables are nothing its just containers that hold data or information. In other, word variable are just identifiers.

Important point about PHP variable

  • It's used for storing information, like name, age place or anything.
  • For example, you create a variable, like $name and store the value 38.
  • The PHP variable names are case-sensitive.

PHP Variable Example


$variable = 15;

$variable_ab = 38;

$sum = $variable + $variable_ab;

echo " First value = ".$variable."
"; echo "Second value = ".$variable."
"; echo "sum = ".$sum; ?>

There are three variable in this example first is "variable", second is "variable_ab" and the third one is "sum". The value of variables displayed, perform an operation and store the value of on another variable named $sum and print.

Example of case sensitive variable



$A=38 ;

echo $a ."<br>";

echo $A;




$a and $A are not same as shown in the above example. Because PHP variable name are case sensitive.

Rules for Variable Declaration

There are some simple rules for creating a PHP variable.

  • A PHP variable must start with ($) dollar sign.
  • A valid PHP variable name start with underscore ( _ ) or alphabet.
  • The rest of variable name consist of alphanumeric character like acb1.
  • PHP variable can any length.
  • Uppercase letter is not equal to the lowercase letter.
  • Not contain spaces

PHP Variable Declaration

A variable is a named memory location that contains data that may be manipulated throughout the execution of the script.

Some valid variable example

  • $name
  • $programming_1
  • $_abc_variable
  • $m

Example of PHP Variable Declaration


$name="welcome to php";
// $12hj=12 //it's wrong 
echo $name;
echo "
"; // $@12hj=12 //its wrong $first_value=12.30; $d1=450; echo "The first value:".$first_value; echo "
"; echo "The second value:".$d1; echo "
"; ?>

You can create PHP variable anywhere by using the dollar sign in our PHP script.

In this example $name is a string variable, $first_value is the integer value and $d1 is a float variable.

Displaying variable values

The fastest way to display the variable value, using print_r statement.

Using print_r

   $name= "john";
print_r( $name ); 

using echo

   $name= "john";
echo 	( $name ); 

Example of variable declaration and initialization


$var="Display the value using echo" ;
$var1=" and print_r statements";
echo $var . $var1;
echo "
"; $first_value=12.30; echo $first_value; $d1=450; echo "
"; print_r($d1); ?>

The following characters are not allowed in variable name

  • asterisk (*)
  • plus(+)
  • hash(#)
  • minus(-)
  • ampersand(&)
  • at the rate(@)

Another way to create variable

It's another method to create a variable using string constant to create a dynamic variable instead of a general variable.


 ${"name_of_variable"} = "value";

Example of variable declaration


$var = "Variable creation=";
${"user"} = "bob";
echo $user;
echo "
"; $a = date("Y/m/d") . "
"; echo "Todays date= "; print_r($a); ?>

In this example we have explained the new way of creation of variable, $user is also a variable like other.

Delete PHP variable

You can use unset() function to destroy the variable. It takes one argument that is a variable name.

Delete variable example


	$brand = "Audi";

	echo $brand;





In the above example $brand is a simple string type variable that hold the value Audi and print the variable and after use we can destroy the variable $brand an using unset function.

After destroying the variable, you cannot use that variable, if you use that variable, PHP displayed the notice like below shown.


Notice: Undefined variable: brand

Check type and length of variable

You can use var_dump( ) function to print the data type and the length of the variable as shown in below.

Check PHP Type Example


	$brand = "Audi";

	var_dump ( $brand );



string(4) "Audi"

You can also use gettype() function to check the type of variable.


We live in this world when we love it.