variables in php



What is variable ?

Variable is a memory location for storing value or we can say variable is a storage area.

  • Variable start with $ sign followed by the variable name in php.
  • Variable name must start with a underscore ( _ ) or letter.
  • In php variables are case sensitive.
  • Variable may contain value or not.
  • Variable can store numbers, string, floating values or arrays.

Variable is a storage area which store the values, values may be numbers, strings , floating values or arrays.

Variable is most important part of programming. If we use one value for multiple times in programs so we need to create one variable and store that value in a variable and we use that variable later in our program.

Generally, student heard or learnt in c programming language about variable that the variable must be declared with data type.
In c language we declare variable with data type, data type define the type of value which stored in variable.

On other hand in php there is no need to declare data type while creating a variable.
we can store number and string value in variable in php.

In php variable declare or we can say create with dollar “$” sign followed by variable name.

Variable example in php

<?php

$no= 13;

?>

Here we have created variable $no with value = 13;

in php there is no need to define data type while creating a variable.

<?php

$name=”meera”;
$city=’Bomaby’;

?>

Above example the $no variable stores the integer value “13” and the $name variable stores the string value “meera”.

In string value both single quote ( ‘ ) and double quote ( ” ) are same in php.

We can also store floating value in variable like below:

<?php

$no= 13.5;

?>


 Variables are case sensitive in php.

Here, we have two variable with same name but the first letter of variable name is different case one is in upper and other is in lower case.

<?php

$name = “meera “;
$Name = “academy”;

echo $name;
echo $Name;

?>

The output is :
meera academy

In above php example variable $name and $Name both are different.


Destroy variable in PHP

In php if we don’t need to use variable later in our program, so we destroy the variable using unset() function.

Example of destroying variables in PHP – unset()

<?php

$name =”meera academy”;
echo $name;

unset($name);

echo $name;

?>

The output of unset() is :
meera academy
Notice: Undefined variable:

Above example we get result string value “meera academy” after we use unset() function for destroy variable $name and then after we try to get value of $name it shows error like : Notice: Undefined variable.

Leave a Reply

Your email address will not be published. Required fields are marked *