How to Check Whether an Array Is Empty in PHP
-
Use
empty()
Function to Check Whether an Array Is Empty in PHP -
Use
sizeof()
Function to Check Whether an Array Is Empty in PHP -
Use
count()
Function to Check Whether an Array Is Empty in PHP -
Use
NOT
Operator to Check Whether an Array Is Empty in PHP
This article will introduce methods to check whether an array is empty in PHP.
- Using
empty()
function - Using
sizeof()
function - Using
count()
function - Using
NOT
operator
Use empty()
Function to Check Whether an Array Is Empty in PHP
We can use the built-in function empty()
to check whether an array is empty. This function checks for all types of variables, including arrays. The correct syntax to use this function is as follows.
empty($variable);
The built-in function empty()
has only one parameter. The detail of its parameter is as follows
Parameters | Description | |
---|---|---|
$variable |
mandatory | It is the variable that we want to check is empty or not. |
This function returns a Boolean
value depending upon the condition of the passed variable. It returns 1 if the variable is empty and returns 0 if the variable is not empty.
The program below shows how we can use this function to check if an array is empty or not.
<?php
$emptyArray = array();
$isEmpty = empty($emptyArray);
echo("The function has returned $isEmpty. \n");
if(empty($emptyArray))
echo("The array is empty.");
?>
We have stored the return value of empty()
function in $isEmpty
variable.
Output:
The function has returned 1.
The array is empty.
Use sizeof()
Function to Check Whether an Array Is Empty in PHP
We can also use the built-in function sizeof()
to check whether an array is empty or not. The sizeof()
function helps in finding the size in numbers. What we will do is that we will find the size of the array. If the size of the array is 0 then our array is empty. The correct syntax to use this function is as follows:
sizeof($array, $mode)
The function sizeof()
accepts two parameters. The detail of its parameters is as follows
Parameters | Description | |
---|---|---|
$array |
mandatory | It is the array whose size we wish to find. It can be a countable as well. |
$mode |
optional | It specifies the mode of the function. It has two values 0 and 1. By default, its value is 0, which means it does not find size recursively. If set to 1, it finds the size of the array recursively. |
The program that checks whether an array is empty using the sizeof()
function is as follows:
<?php
$emptyArray = array();
$size = sizeof($emptyArray);
echo("The size of the array is $size. \n");
if(sizeof($emptyArray) == 0)
echo("The array is empty.");
?>
Output:
The size of the array is 0.
The array is empty.
Use count()
Function to Check Whether an Array Is Empty in PHP
We can also use the built-in function count()
to check whether an array is empty or not. The count()
function is the same in its working as the sizeof()
function. It counts the number of elements of an array or a countable object. What we will do is that we will find the number of elements in the array. If the number of elements in the array is 0 then our array is empty. The correct syntax to use this function is as follows:
count($array, $mode)
The function count()
accepts two parameters. The detail of its parameters is as follows
Parameters | Description | |
---|---|---|
$array |
mandatory | It is the array whose number of elements we wish to find. It can be a countable object as well. |
$mode |
optional | It specifies the mode of the function. It has two values 0 and 1. By default, its value is 0, which means it does not find size recursively. If set to 1, it finds the size of the array recursively. |
The program that checks whether an array is empty using the count()
function is as follows:
<?php
$emptyArray = array();
$numberOfElements = sizeof($emptyArray);
echo("The number of elements in the array is $numberOfElements. \n");
if(count($emptyArray) == 0)
echo("The array is empty.");
?>
Output:
The number of elements in the array is 0.
The array is empty.
Use NOT
Operator to Check Whether an Array Is Empty in PHP
The NOT
(!) operator can also be used to check if an array is empty or not.
The program that checks whether an array is empty using the NOT
operator is as follows:
<?php
$emptyArray = array();
if(!$emptyArray)
echo("The array is empty.");
?>
Output:
The array is empty.