Verification: a143cc29221c9be0

Php check if variable is array

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.

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:

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:

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:

Output:

The array is empty.

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.