Friday 17 May 2013

Lesson 2- Variable in PHP


Variable in PHP

In programming variable is a storage location where data will be stored. In php we declared variable using $(Dollar) symbol.

For Example:-

Declaration of variable:-

$var_data

Note:-var_data is a variable name. it can be anything.

Assign value to the variable:-

$var_data=”isha Malhotra”;

You can also declare and assign value in single line.

Basic program to work with variable and learn more about echo
<?php
$data;
$data="isha Malhotra";
echo "$data";
?>

When I execute this program it will show the following output:-



Figure 1


Variable declaration and definition is must. Suppose if I only declared a variable without assigning the value then it will produce the error.

For Example:-

Following is the code:-
<?php
$data;
echo "$data";
?>
In this code I only declare the variable but didn’t assign any value. The output of this code as follows:-



Figure 2

As you can see that it is showing error that it is undefined variable.
So it is must that if we declare any variable then before using this variable we must have to assign value in it.

If we use single quotation with echo and try to print then it will print text instead of print the value of variable.

For example:-
<?php
$data;
$data="Isha Malhotra";
echo '$data';
?>

The output of this code as follows:-



Figure 3

In this article I described the basic part of variable in php. For any problem and query you can write me mail at info@techaltum.com



No comments:

Post a Comment