PHP Tutorial

PHP Examples

PHP Practice

Check if an array is empty or not in PHP

Learn, how to check whether a given array in an empty array or not in PHP? By Bhanu Sharma Last updated : December 20, 2023

Problem statement

Given an array and we have to check if array is an empty or not using PHP.

Prerequisites

To understand this example, you should have the basic knowledge of the following PHP topics:

PHP - Checking an Empty Array

To check whether an array is empty or not, we can use a built-in function empty(), in other cases where we want to check if a given variable is empty or not, it can also be used. It returns a Boolean response based on the condition that if the given variable contains a non-empty, non-zero value then it returns "false" otherwise, it returns "true".

Syntax

Below is the syntax of the empty() method:

empty ($array);

PHP code to check if an array is empty or not

<?php
// array declaration
$array1 = array("hello", "world");
$array2 = array();

//checking whether arrays are empty or not
if (empty($array1)) {
    echo "array1 is empty<br>";
} else {
    echo "array1 is not empty<br>";
}

if (empty($array2)) {
    echo "array2 is empty<br>";
} else {
    echo "array2 is not empty<br>";
}
?>

Output

array1 is not empty
array2 is empty

PHP Array Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.