Open In App

PHP empty() Function

Last Updated : 24 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The empty() function in PHP checks whether a variable is empty. It returns true if the variable has a value considered “empty,” such as 0, null, false, an empty string, or an unset variable, and false otherwise.

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)

Example: In this example we demonstrates the empty() function, which returns true for variables that are empty, such as 0, “0”, NULL, false, empty arrays, empty strings, and undefined variables.

<?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");

?>

Output
True
True
True
True
True
True
True
True

PHP empty() Function – FAQs

Does empty() return true for 0?

Yes, empty() returns true for the integer 0 as it is considered an empty value.

Does empty() generate a warning for undefined variables?

No, empty() does not generate a warning or notice for undefined variables. It simply returns true if the variable is not set.

How is empty() different from isset()?

empty() checks if a variable has no value (e.g., 0, null, false), while isset() checks if a variable is set and not NULL.

Does empty() treat false as empty?

Yes, empty() treats false as an empty value and returns true for it.

Can empty() be used to check for non-empty values?

Yes, you can use !empty() to check if a variable contains a non-empty value, such as 1, “text”, or a non-empty array.



Next Article

Similar Reads

three90RightbarBannerImg