empty()

Before using variable we can check by using empty function in PHP. The advantage of using empty function is it will not generate any error message if the variable does not exist. This function empty() will return True or False ( Boolean return ) based on status of the variable.
if(empty($my_var)){echo " Variable is empty ";}
else {echo " Variable is not empty ";}
The output of above command is here
Variable is empty
Note that there is no warning message. We will get True as return value from empty() if it is null data or 0 etc.

Here is a list for which we will get True as return value from empty function after checking.
<?php
//For empty string like ""
$my_var="";
if(empty($my_var)){echo " True Variable is empty, ";}
else {echo " False "; } 
// For above code output is True Variable is empty, //

//Zero value string like "0"
$my_var="0";
if(empty($my_var)){echo " True Variable is empty, ";}
else {echo " False "; } 
// For above code output is: True Variable is empty, //


//Zero value of any integer
$my_var=0;
if(empty($my_var)){echo " True Variable is empty, ";}
else {echo " False "; } 
// For above code output is: True Variable is empty, //

//0.0 as float
$my_var=0.0;
if(empty($my_var)){echo " True Variable is empty, ";}
else {echo " False "; } 
// For above code output is: True Variable is empty, //

//Null

$my_var=Null;
if(empty($my_var)){echo " True Variable is empty, ";}
else {echo " False "; } 
// For above code output is: True Variable is empty, //


//False
$my_var=False;
if(empty($my_var)){echo " True Variable is empty, ";}
else {echo " False "; } 
// For above code output is: True Variable is empty, //

//Empty array 
$my_var=array();
if(empty($my_var)){echo " True Variable is empty, ";}
else {echo " False "; } 
// For above code output is: True Variable is empty, //
?>
Example 2
<?php
$my_var="";
echo empty($my_var);
?>
Output is
1
Visitors Rating
Your Rating





Post your comments , suggestion , error , requirements etc here .




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer