I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.
I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)
I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.
I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)
According to phpbench:
Is it worth the effort to calculate the length of the loop in advance?
//pre-calculate the size of array
$size = count($x); //or $size = sizeOf($x);
for ($i=0; $i<$size; $i++) {
//...
}
//don't pre-calculate
for ($i=0; $i<count($x); $i++) { //or $i<sizeOf($x);
//...
}
A loop with 1000 keys with 1 byte values are given.
| count() | sizeof() | |
|---|---|---|
| With precalc | 152 | 212 |
| Without precalc | 70401 | 50644 |
(time in µs)
So I personally prefer to use count() instead of sizeof() with pre calc.
php - Length array in array - Stack Overflow
PHP array size limit?
Is there a PHP equivalent of Javascript's Array.find?
Why don't you make your cities array indexed by id?
More on reddit.comReview and Criticize my DIY Initial Solar Setup Planning and Sizing Attempt
What is the primary function to get the PHP array length?
How do I properly use the PHP array length in a for loop?
";}
How does checking the PHP array length affect performance?
You can use Array Count function count or sizeof function.
try below code:
$array = array(
1,
2,
3,
["thing1", "thing2", "thing3"]
);
echo count($array[3]); //out put 3
echo sizeof($array[3]); //out put 3
You can use the count function:
echo count($array[3]);
However if the array you want to get the length is not always in this same position you could do something like this:
foreach ($array as $element) {
if (is_array($element) {
echo count($element);
}
}