PHP Working with Variables Tutorial

Variables are one of the most important concepts of PHP, learn how to use them here.

PHP  variables

     

If you are planning to start writing in PHP you should  know what a variable is and how to use a variable..
Here is an example of a variable: $jim = "cricket";
What is a variable exactly? A variable is much like a container which can contain String, Integer, Float, Character or Boolean value.

All variables begin with the $ symbol followed immediately by a word which is known as the variables name. No characters or symbols are allowed inside the variables name other then NUMBERS, LETTERS, AND UNDERSCORE SYMBOL (an underscore is this: _ ).
Here is an example of proper syntax
This name would not be allowed: $stea7th owns
This one would though: $stea7th_owns
You must remember though that all variable names are case sensitive, so that means $stea7th and $STEA7TH are not the same. In PHP it is not required to declare any variable before we use it. PHP is loosely typed language, PHP depends on the  value not on the variable, it automagically converts a variable to different datatypes.

Some examples of variable declaration and use are given below:

Example 1:

<?php

$var="This is String value";

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

echo "<b>Datatype of variables are dynamic in nature</b><br/>";

$var=121;

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

?>

Output:

Value of $var is: This is String value
Datatype of variables are dynamic in nature
Value of $var is: 121

Ads