Type of the Variable


Here we will understand, How we can know the type of the variable.
As PHP automatic set the type to the variable as we defined it, But sometime it is neccessary to check the type of the variable.

Suppose you have a condition, that it there is more then 10 Items in store,  you will add them all for calculation of the total payment, but when  the item is only one, there is no need to adding them, as the total cost will be cost of the item.

So you can do by checking the variable type of the variable m If it is array then we will add them all, if this is only a string or Integer, and it is the final cost.

Example you have an Variable $value.

if it is array, we will use array function to check as follow:

<?php
                               $totalcost = 0;
   if( is_array ($value) )
              {
                   foreach ( $value as $single )
                      {
                             $totalcost = $totalcost + $value;
                      }
              } else {
                           $totalcost  =  $value;
              }
?>

Let find out some more in below Program.


##################################
PHP CODE
##################################
<?php

$var1 = "The 2 Sentances 4";
$var2 = 5; // type will be string
$var2 = "5"; // type will be integer

echo $var1;
echo "<br/>";

echo $var2;
echo "<br/>";

settype($var2,'integer');
echo $total = $var1 + $var2;
echo "<br/>";

echo gettype($var1);
echo "<br/>";
echo gettype($var2);
echo "<br/>";
settype($var2,'integer');
echo gettype($var2);
echo "<br/>";

$var1 = "The first Sentances";
$var2 = "The second Sentances";

echo $var1;
echo "<br/>";

echo $var2;
echo "<br/>";

echo $total = $var1 + $var2;
echo "<br/>";

echo gettype($var1);
echo "<br/>";
echo gettype($var2);
echo "<br/>";

// Check the variable type

// check the variable is array or not
if(is_array($var1))
{
echo "Its an Array.";
} else {
echo "Its not an Array.";
}
// check the variable is array or not

echo "<br/>";

// check the variable is float or not
if(is_float($var1))
{
echo "Its an Float.";
} else {
echo "Its not an Float.";
}
// check the variable is float or not

echo "<br/>";

// check the variable is bool or not
if(is_bool($var1))
{
echo "Its an bool.";
} else {
echo "Its not an bool.";
}
// check the variable is bool or not

echo "<br/>";

// check the variable is integer or not
if(is_int($var1))
{
echo "Its an integer.";
} else {
echo "Its not an integer.";
}
// check the variable is integer or not

echo "<br/>";

// check the variable is NULL or not
if(is_null($var1))
{
echo "Its an NULL.";
} else {
echo "Its not an NULL.";
}
// check the variable is NULL or not

echo "<br/>";

// check the variable is numeric or not
if(is_numeric($var1))
{
echo "Its an numeric.";
} else {
echo "Its not an numeric.";
}
// check the variable is numeric or not

echo "<br/>";

// check the variable is string or not
if(is_string($var1))
{
echo "Its an string.";
} else {
echo "Its not an string.";
}
// check the variable is string or not

echo "<br/>";

?>


##################################
OUTPUT OF CODE
##################################
The 2 Sentances 4
5
5
string
integer
integer
The first Sentances
The second Sentances
0
string
string
Its not an Array.
Its not an Float.
Its not an bool.
Its not an integer.
Its not an NULL.
Its not an numeric.
Its an string.

Comments

Popular posts from this blog

Page Unload Show Notification to user

Function Oriented File Upload PHP

Upload File Using PHP - Example For Image