PHP Empty Function With Detailed Explanation

In PHP, empty() function is a special function that allows you to check if the variable is empty or has no value at all.

In addition, this function is significantly different from the isset() function which major function is to check whether a variable is (set).

PHP Empty Function With Detailed Explanation
PHP Empty Function With Detailed Explanation

The truth table of PHP Empty function

The following table below is a representation of empty() function which you can easily identify if they will return true or false.

Expressionempty() Return
$x = “”;true
$x = [];true
$x = true;false
$x = null;true
$x is undefinedtrue

What is PHP empty?

A PHP Empty is a simple function that checks if a variable is empty or not.

This function also returns false when the variable has a value and is not empty. Otherwise, this function will return true.

This function name empty() will be considered your value empty once the variable doesn’t exist or assigned a value that is equal to false, zero, or (empty).

The following are the values that can be considered to be (empty).

  • FALSE
  • NULL
  • array()
  • “0”
  • 0.0
  • “”
  • 0

Syntax:

empty(variable);

For example:

<?php
// Glenn Magada Azuelo
$check = 0.0;

// True because $a is empty
if (empty($check)) {
  echo "Variable 'a' is empty"."\n";
}

// True because $a is set
if (isset($check)) {
  echo "Variable 'a' is set";
}
?>

Output:

Variable 'a' is empty
Variable 'a' is set

The next example will simply show some cases in which the program will return true with the use of the Empty() function.

Note: PHP 5.5 empty only supports variables

For example:

<?php

$empty_values = [
    
    false, // False Value
    NULL,  // Empty Value
    "0",   // 0 As string is empty
    0.0,   // Empty Float
    "",    // Empty String
    0,     // Empty Integer
    [],    // Empty Array
           // else echo
    
];



foreach ($empty_values as $value) {
    var_dump($value);
    echo empty($value) ? "\tis Empty" : "\tis Not Empty";

    echo PHP_EOL . PHP_EOL;
}

Output:

bool(false)
	is Empty

NULL
	is Empty

string(1) "0"
	is Empty

float(0)
	is Empty

string(0) ""
	is Empty

int(0)
	is Empty

array(0) {
}
	is Empty

How to check an array element if exists using PHP empty() function

The empty() function has another great use not just only by checking variable if empty or not but they can also by checking an array elements into an associative array.

This empty() function is pretty useful because it can check an array element if exists and contains a (non-empty) value and generate a warning if the variable is null and has a single parameter.

For example:

<?php

$x = [
    "website"   => "ITSOURCECODE.COM",
    "tutorial"  => "PHP Empty function!!",
    "published" => false,
    "views"     => 0
];


if (!empty($x["website"])) {
    echo $x["website"] . PHP_EOL;
}

if (!empty($x["tutorial"])) {
    echo $x["tutorial"] . PHP_EOL;
}

if (!empty($x["published"])) {
    echo $x["published"] . PHP_EOL;
}

if (!empty($x["views"])) {
    echo $x["views"] . PHP_EOL;
}

?>

Output:

ITSOURCECODE.COM
PHP Empty function!!

Should I use empty PHP?

In some cases, you should have to use the empty() function when you’re not sure of the existence of the variable and data types.

In addition, Once the variable is considered empty it is expected to be (set) in the program, you can use the ($var) rather.

The empty() function is almost equivalent to (! isset($var) || $var == false).

How check string is empty in PHP?

In PHP, checking a string if empty is not quite difficult once the string contains no characters, it can be considered right away as an (empty).

The empty() function can easily filter a PHP and check whether the string is empty or not.

This function is almost used by many programmers or developers if they want to check whether the PHP string is empty or not.

Once the string is empty it will return true.

Summary

This article discusses empty() function.

It also tackles how to check if an array element exists, the use of an empty function, and how to check if a string is empty.

I hope this lesson has helped you learn a lot.

Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment