To check if an array is empty in PHP, you can use the empty()
function or check its count()
function result.
In this example,
numbers
with some elements.empty()
function and store the result in the variable isEmpty
.<?php
$numbers = [1, 2, 3];
$isEmpty = empty($numbers);
echo 'Array is empty: ' . ($isEmpty ? 'true' : 'false');
?>
Array is empty: false
In this example,
emptyArray
.empty()
function and store the result in the variable isEmpty
.<?php
$emptyArray = [];
$isEmpty = empty($emptyArray);
echo 'Array is empty: ' . ($isEmpty ? 'true' : 'false');
?>
Array is empty: true
In this tutorial, we learned How to check if an Array is Empty in PHP language with well detailed examples.