Which of the options is the most correct for checking a php array for the absence of elements in it?
if ($ arr) {...}
if (empty ($ arr)) {...}
if (isset ($ arr [0])) {...}
if (count ($ arr)) {...}
- This answer is verified through a complex array microtest with 40014 elements and PHP 7.1
- The script was executed on the local machine (Open Server 5.2).
- Metering took place through the php microtime () function
- The result was checked three times
- if (count ($ arr)) {…}: 0.0010001659393311 sec.
- if (sizeof ($ arr)) {…}: 0 sec.
- if ($ array) {…}: 0 sec.
- if (empty ($ arr)) {…}: 0 sec.
- if (array_shift ($ arr)) {…}: 0 sec.
- if ($ arr [0]) {…}: 0 sec.
- if (count ($ arr)) {…}: 0 sec.
- if (sizeof ($ arr)) {…}: 0.0010011196136475 sec.
- if ($ array) {…}: 0 sec.
- if (empty ($ arr)) {…}: 0 sec.
- if (array_shift ($ arr)) {…}: 0 sec.
- if ($ arr [0]) {…}: 0 sec.
- if (count ($ arr)) {…}: 0.00099992752075195 sec.
- if (sizeof ($ arr)) {…}: 0 sec.
- if ($ array) {…}: 0 sec.
- if (empty ($ arr)) {…}: 0 sec.
- if (array_shift ($ arr)) {…}: 0 sec.
- if ($ arr [0]) {…}: 0 sec.
- The code is more readable – “If the variable is equal to an empty array”;
- Type checking is in progress and you are actually comparing the array;
- No functions are used, only comparison, which means this code will work a little faster;
Answer 1, authority 100%
Yes, all, in general, are correct. Only in the first and fourth cases there will be a type conversion to bool, and the third is suitable only for indexed arrays. There is another option sizeof ($ arr)
.
Answer 2, authority 19%
Perhaps the best option is if ($ arr === Array ()) {...}
Answer 3, authority 12%
ANSWER :
Checking an empty array using the count and sizeof functions is not worth it, as it wastes time on processing. scanty time, but wasting.
The best options are:
1. if ($ arr) {...} // Check for elements inside
2.if (empty ($ arr)) {...} // Check the existence of a variable with an array and the presence of elements inside
3.if (array_shift ($ arr)) {...} // Check if the first element is present
4. if ($ arr [0]) {...} // Check for the presence of a zero element, for an index array that starts from 0
RESEARCH:
STUDY RESULTS:
First launch
Second launch
Third run
WHAT I DO:
For myself I use if ($ arr) {…} and if ($ arr [0]) {…}, since there is no need to call additional functions – wrappers
Answer 4, authority 6%
if ($ array === []) {
…
}
More optimal because:
Answer 5
I am using if ((bool) array_filter ($ array))
Answer 6
The documentation says that a array with no elements is interpreted as false .
https: //www.php. net / manual / en / language.types.boolean.php # language.types.boolean.casting
var_dump ((bool) array (12)); // bool (true)
var_dump ((bool) array ()); // bool (false)
So I made sure that the fastest way
if ($ arr) {}
$ arr = [];
$ a1000 = range (0, 1000); // 0,1,2 ... 1000
for ($ i = 0; $ i & lt; 1000000; $ i ++) {
$ j = rand (0.20);
if ($ j === 0) {
$ arr [] = [];
} else {
shuffle ($ a1000);
$ arr [] = array_slice ($ a1000,0, $ j);
}
}
$ t = microtime (true);
$ n = count_not_empty1 ($ arr);
$ t = microtime (true) - $ t;
echo 'if ($ a): N ='. $ n. ' T = '. Round ($ t * 1000,4).' ms'.PHP_EOL;
$ t = microtime (true);
$ n = count_not_empty2 ($ arr);
$ t = microtime (true) - $ t;
echo 'if ($ a! == []): N ='. $ n. ' T = '. Round ($ t * 1000,4).' ms'.PHP_EOL;
$ t = microtime (true);
$ n = count_not_empty3 ($ arr);
$ t = microtime (true) - $ t;
echo 'if (! empty ($ a)): N ='. $ n. ' T = '. Round ($ t * 1000,4).' ms'.PHP_EOL;
$ t = microtime (true);
$ n = count_not_empty4 ($ arr);
$ t = microtime (true) - $ t;
echo 'if (count ($ a)): N ='. $ n. ' T = '. Round ($ t * 1000,4).' ms'.PHP_EOL;
function count_not_empty1 (& amp; $ arr)
{
$ n = 0;
for ($ i = 0; $ i & lt; 1000000; $ i ++) {
if ($ arr [$ i]) {
$ n ++;
}
}
return $ n;
}
function count_not_empty2 (& amp; $ arr)
{
$ n = 0;
for ($ i = 0; $ i & lt; 1000000; $ i ++) {
if ($ arr [$ i]! == []) {
$ n ++;
}
}
return $ n;
}
function count_not_empty3 (& amp; $ arr)
{
$ n = 0;
for ($ i = 0; $ i & lt; 1000000; $ i ++) {
if (! empty ($ arr [$ i])) {
$ n ++;
}
}
return $ n;
}
function count_not_empty4 (& amp; $ arr)
{
$ n = 0;
for ($ i = 0; $ i & lt; 1000000; $ i ++) {
if (count ($ arr [$ i])) {
$ n ++;
}
}
return $ n;
}
if ($ a): N = 952522 T = 117.7402 ms
if ($ a! == []): N = 952522 T = 122.844 ms
if (! empty ($ a)): N = 952522 T = 125.299 ms
if (count ($ a)): N = 952522 T = 128.9139 ms