PHP | empty() Function

The empty() function is an inbuilt function in PHP which is used to check whether a variable is empty or not.

Syntax:

bool empty ( $var )

Parameter: This function accepts a single parameter as shown in above syntax and described below.

  • $var: Variable to check whether it is empty or not.

Note: Below version of PHP 5.5, empty() only supports variables, anything other will result in a parse error. The following statement will not work empty(trim($var)). Instead, use trim($name) == false.

Return Value: It returns FALSE when $var exists and has a non-empty, non-zero value. Otherwise it returns TRUE.

These values are considered to be as an empty value:

  • “” (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • “0” (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)

Below program illustrate the empty() function in PHP:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to demonstrate working of empty() function
$var1 = 0;
$var2 = 0.0;
$var3 = "0";
$var4 = NULL;
$var5 = false;
$var6 = array();
$var7 = "";
  
// for value 0 as integer
empty($var1) ? print_r("True\n") : print_r("False\n");
  
// for value 0.0 as float
empty($var2) ? print_r("True\n") : print_r("False\n");
  
// for value 0 as string
empty($var3) ? print_r("True\n") : print_r("False\n");
  
// for value Null
empty($var4) ? print_r("True\n") : print_r("False\n");
  
// for value false
empty($var5) ? print_r("True\n") : print_r("False\n");
  
// for array
empty($var6) ? print_r("True\n") : print_r("False\n");
  
// for empty string
empty($var7) ? print_r("True\n") : print_r("False\n");
  
// for not declare $var8
empty($var8) ? print_r("True\n") : print_r("False\n");
  
?>

chevron_right


Output:

True
True
True
True
True
True
True
True

Reference : http://php.net/manual/en/function.empty.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.