[an error occurred while processing this directive]

PHP variables 2

                         

Variables in php are dynamically declared and used. We use "$" sign before any variable. In php a variable is of dynamic datatype that means we can assign any  kind of value to a variable. Following examples will exemplify the use of variable

Variables naming conventions are similar to any other language like first letter of the variable name should start with any alphabet or "_ " underscore. Variable name should contain only capital letter or small letter or integers or underscore symbol. Variable name with more than one word should separated with underscore symbol or the first alphabet of the second should be in capital letter.

Example 1:

<?php

$var="This is a string value";//declaration as string variable

echo "Value of \$var is: ".$var."<br/>";

$var=23232;//declaration as integer variable

echo "Value of \$var is: ".$var."<br/>";

$var=23.23;//declaration as float variable

echo "Value of \$var is: ".$var."<br/>";

$var=true;

echo "Value of \$var is: ".$var."<br/>";

$var='c';

echo "Value of \$var is: ".$var."<br/>";

?>

Output:

Value of $var is: This is a string value
Value of $var is: 23232
Value of $var is: 23.23
Value of $var is: 1
Value of $var is: c

From the above example we can easily understand that the datatype of a variable is dynamic in nature. A variable is capable of storing any kind of value right from String, Integer, Float etc.

Sometimes we need to print Double quote, Single quote, Variable names as a statement, in this case we should use \ sign.

Example 2:

<?php

$var="Any Value";//declaration as string variable

echo "With '\' sign output is= \$var"."<br/>";

echo "Without '\' sign output is=$var"."<br/>";

echo"\"This text is within double quote\""."<br/>";

?>

Output:

With '\' sign output is= $var
Without '\' sign output is= Any Value
"This text is within double quote"

                         

[an error occurred while processing this directive]