To check if an array is empty in PHP, you can use the empty()
function or the count()
function. Here are the examples:
Using empty()
function:
$array = array(); // or $array = []
if (empty($array)) {
echo "Array is empty";
} else {
echo "Array is not empty";
}
Using count()
function:
$array = array(); // or $array = []
if (count($array) == 0) {
echo "Array is empty";
} else {
echo "Array is not empty";
}
Both empty()
and count()
functions will return true
if the array is empty and false
otherwise.