Check if an array is empty or not in PHP

Check if an array is vacant or not in PHP

Here, we will figure out how to check whether a given array in a vacant array or not in PHP?

Given an array and we need to check if the array is vacant or not utilizing PHP.

To check whether an array is unfilled or not, we can utilize an inherent function vacant(), in different situations where we need to check if a given variable is vacant or not, it can likewise be utilized. It restores a Boolean reaction dependent on the condition that on the off chance that the given variable contains a non-void, non-zero value then it returns “False” else, it returns “genuine“.

Syntax:

    empty ($var);
    empty ($array);

PHP Code:

<?php
    // array declaration
    $array1 = array('hello', 'world');
    $array2 = array();
    
    //checking whether arrays are empty or not
    if (empty($array1)) {
        echo "array1 is empty\n";
    } else {
        echo "array1 is not empty\n";
    }
    
    if (empty($array2)) {
        echo "array2 is empty\n";
    } else {
        echo "array2 is not empty\n";
    }
?>

Output:

array1 is not empty
array2 is empty

Leave a Comment

error: Alert: Content is protected!!