An empty can sometimes cause software crash or unexpected outputs. To this, it is better to whether an is empty or not beforehand. There are various methods and functions available in to check whether the defined or given is an empty or not. Some of them are given below:

  1. Using empty() Function: This function determines whether a given variable is empty. This function does not return a warning if a variable does not exist.

    Syntax:

    bool empty( $var )

    Example:


    <?php 

      

      

    $empty_array = array();

      

    if(!empty($non_empty_array))

        echo "Given Array is not empty <br>";

      

    if(empty($empty_array))

        echo "Given Array is empty";

    ?>

    Output:
    Given Array is not empty 
    Given Array is empty
  2. Using count Function: This function counts all the in an array. If number of in array is zero, then it will display empty array.

    Syntax:

    int count( $array_or_countable )

    Example:

    <?php 

       

    $empty_array = array();

       

    if(count($empty_array) == 0)

        echo "Array is empty";

    else

        echo "Array is non- empty";

    ?>

  3. Using sizeof() function: This method check the size of array. If the size of array is zero then array is empty otherwise array is not empty.

    Example:

    <?php 

       

    $empty_array = array();

       

    if( sizeof($empty_array) == 0 )

        echo "Empty Array";

    else

        echo "Non-Empty Array";

    ?>




If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Article Tags :

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.







Source link
thanks you RSS link
( https://www.geeksforgeeks.org/how-to-check-whether-an-array-is-empty-using-php/)

LEAVE A REPLY

Please enter your comment!
Please enter your name here