PHP array: count or sizeof? - Stack Overflow
html - Sizeof array in php not working in the for loop - Stack Overflow
How to know the size of an array? (C)
How much RAM does a python list take with 6000 elements where each element is a string?
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.
You're creating an empty array and then counting the size of it. Why would you expect anything but a count of zero?
What you really want is the size of the que array:
$length = count($_POST['que']);
But that makes some your code unnecessary since this is already an array.
for($j=1; $j<$length; $j++) {
// $question = $_POST['que']; UNNECESSARY
if($_POST['que'][$j] != "") {
$bla .= $j.'This is good<br/><br/>';
}
}
<?php
$bla = "";
$question = $_POST['que'];
$length = count($question);
for($j=1; $j<$length; $j++) {
if($question[$j] != "") {
$bla .= $j.'This is good<br/><br/>';
}
}
echo $bla;
?>