PHP Check For Empty Array | Check if an Array is Empty or Not in PHP

Summary: In this article, we learn PHP Check For Empty Array means that we will check Whether an Array Is Empty in PHP with example. this article will introduce different ways to check whether an array is empty in PHP such as using an empty() function, using sizeof() function, using count() function, using the NOT operator. Let’s understand in detail this article.

PHP Check For Empty Array Overview:

This tutorial covers a different way to check whether an array is empty in PHP:

TABLE OF CONTENT
1. Use count() Function to Check Whether an Array Is Empty
2. Use empty() Function to Check Whether an Array Is Empty
3. Use sizeof() Function to Check Whether an Array Is Empty
4. Use NOT Operator to Check Whether an Array Is Empty

1. Use count() Function to Check Whether an Array Is Empty in PHP

To check whether an array is empty or not we can use the PHP built-in function count(). we learned in the previous article, that the PHP count() function is the same in its working as the PHP sizeof() function. furthermore, both function in PHP counts the number of elements of an array or a countable object. we will use the count function to find the number of elements in the array. If the number of elements in the array is zero (0) then our array is empty. the correct syntax to use this function is as follows:

count($array, $mode)

The count() function accepts two parameters. one is mandatory and another parameter is optional, the detail of its parameters is as follows:

Name Description Type
$array It is a mandatory parameter. Specifies the array or object to count. Array
mode It is a optional parameter. Sets the mode of the function.
Possible values:

  • COUNT_RECURSIVE (or 1): here the count() function counts the array recursively. it is very useful to counting all the elements of a multidimensional array.
  • The default value is 0.
Integer
.
Important Note:
Return value: The number of elements in $array.
Value Type: Array.

The program that checks whether an array is empty using the count() function is as follows:

The num. of elements in the array is 0.
The array is empty.

2. Use empty() Function to Check Whether an Array Is Empty

PHP Check For Empty, you can use the PHP built-in function empty() to check whether an array is empty. furthermore, this function checks for all types of variables, including arrays. The syntax to use this function is as follows.

empty($variable);

The PHP built-in function empty() has only one parameter. which is a mandatory parameter value, The detail of its parameter is as follows:

Name Description
$variable It is a mandatory parameter. It is the variable that we want to check is empty or not.

This PHP built-in function returns a Boolean value, which is depending upon the condition of the passed variable. furthermore, it returns 1(one) if the variable is empty otherwise returns 0(zero) if the variable is not empty.
The given below program shows that how we can use this function to check if an array is empty or not.


We have stored the return value of empty() function in $isEmpty variable.

This function has returned 1.
The array is empty.

3. Use sizeof() Function to Check Whether an Array Is Empty

We will use PHP built-in function sizeof() to check whether an array is empty or not. The sizeof() function will works as the same count() function. It helps in finding the size in numbers. What we’ll do is that we’ll find the size of the array. furthermore, if the size of the array is zero (0) then our array is empty. The following function syntax to use is as follows:

sizeof($array, $mode)

The function sizeof() accepts two parameters. one mandatory parameter and other parameter is optional, the detail of its parameters is as follows:

Name Description Type
$array It is a mandatory parameter. and specifies the array whose size we want to find. It can be a countable as well.
mode It is an optional parameter. which specifies the mode of the function. It has two values 0 and 1. furthermore, by default, its value is zero (0), which means it doesn’t find the size recursively. If set to 1, it finds the size of the array recursively.

The given below example to checks whether an array is empty using the PHP sizeof() function is as follows:


We have stored the return value of empty() function in $isEmpty variable.

The size of the array is 0.
The array is empty.

4. Use NOT Operator to Check Whether an Array Is Empty

In PHP we can also use the NOT (!) operator to check if an array is empty or not.
The program that checks whether an array is empty or not using the PHP NOT operator is as follows:


We have stored the return value of empty() function in $isEmpty variable.

The array is empty.

Conclusion:

In this article, you have learned PHP Check For Empty Array means that we will check Whether an Array Is Empty in PHP with example. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!