PHP Menu

Variables Declaration in PHP

A variable is a named memory location in which we can store values for the particular program.In other words, Variable is a name of memory location and used to hold the value.

Creating Variables

In PHP, a variable is created using $ symbol followed by variable name.

 $variablename = value; 

Rules for naming variable:

  • Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore, i.e. the variable name can't be starts with numbers or special symbols.

    $a = "Study";			---> Valid declaration
    $_b = "Glance";			---> Valid declaration
    $3c = "Hello";			---> Invalid declaration
    $*d = 25;				---> Invalid declaration

  • In PHP, The variable names are case - sensitive, so the variable name $a is different from $A, i.e. $a != $A

    $a = "Study";
    $A = "Glance";

    where both variables ($a and $A) are different.

In PHP, We don't need to declare explicitly variable, when we assign any value to the variable that variable is declared automatically.

In PHP, We don't need to specify the type of variable because PHP is a loosely typed language. I.e. In loosely typed language no need to specify the type of variable because the variable automatically changes it's datatype based on the assigned value.


$str = "Study Glance";			//string type variable		
$x = 250;			//integer type variable
$y = 14.6;			//float type variable

Example: "VarDemo.php"


<title>Variables Demo</title>
<body bgcolor="#fff">
<h1>Variable Demonstration</h1>
"Study Glance";
$x 112;
$y 45.12;
"String is : $str<br/>";
"Integer is : $x <br/>";
"Float is : $y <br/>";

Output :

Next Topic :Datatypes in PHP