How to check if an array is empty - true or false

My question is how to set a condition for a function that returns trueor false, if I'm sure the array is empty, but isset()passed it.

$arr = array();

if (isset($arr)) {
    return true;
} else {
    return false;
}

In this form, returns bool(true)and var_dumpshows array (0) {}.

+5
source share
5 answers

If it is an array, you can just use ifor just a boolean expression. An empty array evaluates to FALSE, any other array to TRUE( Demo ):

$arr = array();

echo "The array is ", $arr ? 'full' : 'empty', ".\n";

PHP manually beautifully lists what is false, not .

+13
source

empty(), .

if (empty($arr)) {
  // it empty
} else {
  // it not empty
}
+6

You can also check how many elements are in the array using the function count:

$arr = array();

if (count($arr) == 0) {
  echo "The array is empty!\n";
} else {
  echo "The array is not empty!  It has " . count($arr) . " elements!\n";
}
+2
source

use an empty property as

if (!empty($arr)) {
    //do what u want if its not empty
} else {
    //do what if its empty
}
0
source

All Articles