Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if array is empty - true or false

Tags:

arrays

php

I have a question, how do I put a condition for a function that returns true or false, if I am sure that 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_dump shows array (0) {}.

like image 652
jam9 Avatar asked Sep 16 '11 15:09

jam9


People also ask

Is empty array True or false?

arrays are objects, objects are truthy. just ask for array. length, if not zero, it will be truthy. when you explicitly convert to Boolean, the array turns into an empty string first, then the empty string turns into false.

Is empty array true or false PHP?

For example, in PHP, empty arrays are falsy, but in JavaScript arrays are always truthy.

Is empty array true in JavaScript?

The array can be checked if it is empty by using the array. length property. This property returns the number of elements in the array. If the number is greater than 0, it evaluates to true.


1 Answers

If it's an array, you can just use if or just the logical expression. An empty array evaluates to FALSE, any other array to TRUE (Demo):

$arr = array();

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

The PHP manually nicely lists what is false and not.

like image 91
hakre Avatar answered Oct 07 '22 16:10

hakre