Verification: a143cc29221c9be0

Php check if any value in array

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:

$Empty_Array = array();

$numOfElements = sizeof($Empty_Array);

echo("The num. of elements in the array is $numOfElements. \n");

if(count($Empty_Array) == 0)

echo("The array is empty.");

?>

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.

$Empty_Array = array();

$isEmpty = empty($Empty_Array);

echo("This function has returned $isEmpty. \n");

if(empty($Empty_Array))

echo("The array is empty.");

?>


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:

$Empty_Array = array();

$size = sizeof($Empty_Array);

echo("The size of the array is $size. \n");

if(sizeof($Empty_Array) == 0)

echo("The array is empty.");

?>


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

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

RANDOM BOOLEANS

  • in_array: 10 bools = 0.16 sec
  • foreach: 10 bools = 0.122 sec
  • array_unique: 10 bools = 0.274 sec
  • foreach_bin: 10 bools = 0.095 sec
  • in_array_bin: 10 bools = 0.073 sec
  • array_sum: 10 bools = 0.074 sec
  • array_filter: 10 bools = 0.118 sec

  • in_array: 100 bools = 0.153 sec
  • foreach: 100 bools = 0.122 sec
  • array_unique: 100 bools = 2.3451 sec
  • foreach_bin: 100 bools = 0.094 sec
  • in_array_bin: 100 bools = 0.074 sec
  • array_sum: 100 bools = 0.126 sec
  • array_filter: 100 bools = 0.228 sec

  • in_array: 1000 bools = 0.154 sec
  • foreach: 1000 bools = 0.149 sec
  • array_unique: 1000 bools = 32.6659 sec (!!)
  • foreach_bin: 1000 bools = 0.075 sec
  • in_array_bin: 1000 bools = 0.074 sec
  • array_sum: 1000 bools = 0.8771 sec
  • array_filter: 1000 bools = 1.4021 sec