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) {}
.
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.
For example, in PHP, empty arrays are falsy, but in JavaScript arrays are always truthy.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With