Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Php PHP variables 2

 
 

Share on Google+Share on Google+

PHP variables 2

Advertisement
Variables in php are dynamically declared and used.

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"
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2009

Related Tutorials

Discuss: PHP variables 2  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
seetal singh
December 29, 2011
regarding php language

i interested for this language pls give me source file and defined me how we write coading on editor thanks rgd.... seetal singh
seetal singh
December 29, 2011
regarding php language

i interested for this language pls give me source file and defined me how we write coading on editor thanks rgd.... seetal singh
sudhir yadav
February 2, 2012
php

plz tell me making cms. in detail.
DMCA.com