Variable Functions in PHP


Variable function allows you to manipulate variables such as determine type of variable, set type of variable, display value of variables etc…
Variable functions are explained in detail below:

(1) gettype:

gettype () function accepts variable as an argument and returns the data type of that variable.
Syntax:
gettype (VariableName)

<? php
$a=2;
$b=2.5;
$c="Hello";
$d=true;
echo gettype($a)."<br/>";
echo gettype($b)."<br/>";
echo gettype($c)."<br/>";
echo gettype($d)."<br/>";
?>


Output:
Integer
Double
String
Boolean


     

(2) settype:

settype () function accepts a variable as an argument and set it to the specific data type passed as an argument.
Thus settype () function is used to set variable to specific data type.
It returns a Boolean value. If variable is set successfully to specific type then it returns 1 (true) otherwise it returns 0 (false).
Syntax:
Settype (VariableName, DataType)

<?php
$a;
settype($a,"Integer");
echo gettype($a)."<br/>";
echo $a;
?>


Output:
Integer
0


(3) isset:

isset () function accepts variable as an argument and determines weather the variable exist and it is assigned value or not.
It returns Boolean value. If variable is exist and assigned value then it returns true otherwise it returns false.
Syntax:
isset (VariableName)

<?php
$a=5;
if (isset($a))
echo "Variable is assigned value";
else
echo "Variable is not assigned value";
?>


Output:
Variable is assigned value


(4) unset:

unset () function accepts a variable as an argument and destroy that variable from memory.
Syntax:
unset (VariableName)

<?php
$a=5;
echo $a;
unset($a);
echo $a;
?>
Output
5
Notice: Undefined variable: a in C:\wamp\www\Variable.php on line 5


(5) strval:

strval () function accepts variable as an argument and returns string value of that variable.
It returns a value of type string.
Syntax:
Strval (Variable Name);

<?php
$a=12;
$b=12.35;
$c="Hello";
$d=true;
$a=strval($a);
$b=strval($b);
$c=strval($c);
$d=strval($d);
echo gettype($a)."<br/>";
echo gettype($b)."<br/>";
echo gettype($b)."<br/>";
echo gettype($d)."<br/>";
?>
Output:
string
string
string
string


(6) floatval:

floatval () function accepts variable as an argument and returns float value of the variable.
If you pass variable of type string as an argument then it returns 0.
If you pass variable of type string whose starting characters are digit then it returns only digit part of the string. If string contains digits in between or at the end then it returns 0.
Syntax:
floatval (Variable Name);

<?php
$a=12.23;
$b="12.35";
$c="12.25Hello";
$d="Hello12.25";
$e="Hello";
echo floatval($a)."<br/>";
echo floatval($b)."<br/>";
echo floatval($c)."<br/>";
echo floatval($d)."<br/>";
echo floatval($e)."<br/>";
?>
Output:
12.23
12.35
12.25
0
0


(7) intval:

intval () function accepts variable as an argument and returns integer value of the variable.
If you pass variable of type string as an argument then it returns 0.
If you pass variable of type string whose starting characters are digit then it returns only integer value of the digit part of the string. If string contains digits in between or at the end then it returns 0.
Syntax:
intval (Variable Name);

<?php
$a=12.25;
$b="12.35";
$c="12.25Hello";
$d="Hello12.25";
$e="Hello";
echo intval($a)."<br/>";
echo intval($b)."<br/>";
echo intval($c)."<br/>";
echo intval($d)."<br/>";
echo intval($e)."<br/>";
?>
Output:
12
12
12
0
0


(8) print_r:

print_r () accepts variable as an argument and display it in a human readable format.
It is useful for displaying contents of an array. This function display value of each element of array along with its index so user can easily read the contents of array.
Syntax:
print_r (Variable Name);

<?php
$b=array("Pankaj","Keyur","Mitul");
print_r($b)."<br/>";
?>
Output:
Array ( [0] => Pankaj [1] => Keyur [2] => Mitul )

Download Projects


Download Programs