PHP empty

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : empty() function

Description

The empty() function is used to check whether a variable is empty or not.

Version

(PHP 4 and above)

Syntax

empty(var_name)

Name

Description

Required /
Optional

Type

var_name

The variable being checked.

Required

Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

FALSE if var_name has a non-empty and non-zero value.

Value Type : Boolean

List of empty things :

  • "0" (0 as a string)

  • 0 (0 as an integer)

  • "" (an empty string)

  • NULL

  • FALSE

  • "" (an empty string)

  • array() (an empty array)

  • $var_name; (a variable declared but without a value in a class)

Example :

  1. $ivar1=0;  

  2. $istr1='Learning empty';  

  3. if (emptyempty($ivar1))  

  4. {  

  5. echo '$ivar1'." is empty or 0. 
    ";  

  6. }  

  7. else  

  8. {  

  9. echo '$ivar1'." is not empty or 0. 
    ";  

  10. }  

  11. if (emptyempty($istr1))  

  12. {  

  13. echo '$istr1'." is empty or 0. 
    ";  

  14. }  

  15. else  

  16. {  

  17. echo '$istr1' ." string is not empty or 0. 
    ";  

  18. }  

  19. ?>        

Output :

$ivar1 is empty or 0.
$istr1 string is not empty or 0.