PHP Variable function
1- empty()

This function check whether a variable is empty or not.

Note : - Return false if variable has a non_empty and non-zero value. Return true if variable has empty or zero.

Return Type:- bool

<?php
$var = 30;

if (empty($var))
{
echo "Varible is empty";
}
else
{
echo "Varible is not empty";
}
?>

Output:-
Varible is not empty
2- isset()

This function check whether variable exists or not. Return True if variable exists otherwise return False.

Note : - isset function is very usefull for check $_Post['btnsubmit'], $_Get['btnsubmit'] etc variable.

Return Type:- bool

<?php

$var = 30;

if (isset($var))
{
echo "Varible is exist";
}
else
{
echo "Varible is not exist";
}

?>

Output :-
Varible is exist

3- unset()

This function destroy variables.

Note :- You can use unset function to destroy session varaibles.

Return Type :- void

<?php

$var = 30;

echo $var;

unset($var);

echo $var;

?>

Output :-
30

4- print_r()

This function display information about variable.

Return Type :- bool

<?php
$my_array = array(5,6,7,8);
print_r($my_array);
?>

Output :-
Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 )

5- intval()

This function returns the integer value of the variable.

Return Type :- int

<?php
echo intval(45.67);
echo "<br>";
echo intval(-67.45);
?>


Output :
45
-67

6- gettype()

 This function returns the type of variable.

Return Type :- string

<?php
$x="welcome";
$y=56;
$z=56.34;
echo gettype($x);
echo "<br>";
echo gettype($y);
echo "<br>";
echo gettype($z);
?>

Output :
string
integer
double

7- settype()

This function returns convert variable datatype.

Return Type :- bool

<?php
$x="welcome";
$y=56;
$z=56.34;
settype($x,"integer");
echo gettype($x);
echo "<br>";
settype($y,"boolean");
echo gettype($y);
echo "<br>";
settype($z,"double");
echo gettype($z);
?>

Output :
integer
boolean
double

8- strval()

This function convert a value of the variable to the string.

Return Type :- string

Example :-

<?php
$num=5;
$str=strval($num);
echo gettype($num)."<br>";
echo gettype($str);
?>

Output :-
integer
string

9- var_dump()

This function provide us dumps information of variable.

Return Type :- void

Example :-

<?php
$x="This is test string";
$a = array(34, 62, array("sun", "mon", "tue"));
var_dump($x);
var_dump($a);
?> 

Output :-

string 'This is test string' (length=19)
array
  0 => int 34
  1 => int 62
  2 => 
    array
      0 => string 'sun' (length=3)
      1 => string 'mon' (length=3)
      2 => string 'tue' (length=3)

Previous
Next