Q:

Check if an array is empty or not in PHP

0

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

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:

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now