How to check whether an array is empty using PHP? - GeeksforGeeks

31 Jul, 2021
An empty array can sometimes cause software crash or unexpected outputs. To avoid this, it is better to check whether an array is empty or not beforehand. There are various methods and functions available in PHP to check whether the defined or given array is an empty or not. Some of them are given below:
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   // Declare an array and initialize it$non_empty_array = array('URL' => 'https://www.geeksforgeeks.org/');  // Declare an empty array$empty_array = array();  // Condition to check array is empty or notif(!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
Using count Function: This function counts all the elements in an array. If number of elements in array is zero, then it will display empty array.Syntax:int count( $array_or_countable )Example:<?php    // Declare an empty array $empty_array = array();   // Function to count array // element and use conditionif(count($empty_array) == 0)    echo "Array is empty";else    echo "Array is non- empty";?>Output:Array is empty
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    // Declare an empty array$empty_array = array();   // Use array index to check// array is empty or notif( sizeof($empty_array) == 0 )    echo "Empty Array";else    echo "Non-Empty Array";?>Output:Empty Array

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   // Declare an array and initialize it$non_empty_array = array('URL' => 'https://www.geeksforgeeks.org/');  // Declare an empty array$empty_array = array();  // Condition to check array is empty or notif(!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

Syntax:
bool empty( $var )
Example:
<?php   // Declare an array and initialize it$non_empty_array = array('URL' => 'https://www.geeksforgeeks.org/');  // Declare an empty array$empty_array = array();  // Condition to check array is empty or notif(!empty($non_empty_array))    echo "Given Array is not empty <br>";  if(empty($empty_array))    echo "Given Array is empty";?>
Given Array is not empty Given Array is empty

Using count Function: This function counts all the elements in an array. If number of elements in array is zero, then it will display empty array.Syntax:int count( $array_or_countable )Example:<?php    // Declare an empty array $empty_array = array();   // Function to count array // element and use conditionif(count($empty_array) == 0)    echo "Array is empty";else    echo "Array is non- empty";?>Output:Array is empty

Syntax:
int count( $array_or_countable )
Example:
<?php    // Declare an empty array $empty_array = array();   // Function to count array // element and use conditionif(count($empty_array) == 0)    echo "Array is empty";else    echo "Array is non- empty";?>
Array is empty

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    // Declare an empty array$empty_array = array();   // Use array index to check// array is empty or notif( sizeof($empty_array) == 0 )    echo "Empty Array";else    echo "Non-Empty Array";?>Output:Empty Array

Example:
<?php    // Declare an empty array$empty_array = array();   // Use array index to check// array is empty or notif( sizeof($empty_array) == 0 )    echo "Empty Array";else    echo "Non-Empty Array";?>
Empty Array

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Picked
PHP
PHP Programs
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to run JavaScript from PHP?
How to pass JavaScript variables to PHP ?
Download file from URL using PHP
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
How to run JavaScript from PHP?
How to pass JavaScript variables to PHP ?
Split a comma delimited string into an array in PHP