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.

format:
 $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.

    Example:
    $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

    Example:
    $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.

Example:

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



Example: "VarDemo.php"


<html>

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

Output :

image
Next Topic :Datatypes in PHP