[PHP-DEV] PHP 4.0 Bug #4433: sizeof($array) isn't accurate From: bens <email protected>
Date: 05/12/00

From: bens <email protected>
Operating system: RH Linux 6.0 (2.2 kernel)
PHP version: 4.0 Release Candidate 1
PHP Bug Type: Misbehaving function
Bug description: sizeof($array) isn't accurate

<?php

function collapsearray ($example)
{
### Copy array A>B

################## See Note Below! ####################
for ($i=0; $i <= sizeof($example); $i++)
        { if ($example[$i] != "")
                {
                $output[$j]=$example[$i];
                $j=$j+1;
                }
        }

### Kill array A

unset($example);

### Copy array B>A
for ($i=0; $i < sizeof($output); $i++)
        {
        $example[$i]=$output[$i];
        }

return($example);
} ## end of function collapsearray

################## Start of program code #################

<FORM ACTION=test3.php METHOD=post>
<BR>0<INPUT TYPE=CHECKBOX Value=0 NAME=check[]><INPUT TYPE=TEXT VALUE=A NAME=ex$
<BR>1<INPUT TYPE=CHECKBOX Value=1 NAME=check[]><INPUT TYPE=TEXT VALUE=B NAME=ex$
<BR>2<INPUT TYPE=CHECKBOX Value=2 NAME=check[]><INPUT TYPE=TEXT VALUE=C NAME=ex$
<BR>3<INPUT TYPE=CHECKBOX Value=3 NAME=check[]><INPUT TYPE=TEXT VALUE=D NAME=ex$
<BR>4<INPUT TYPE=CHECKBOX Value=4 NAME=check[]><INPUT TYPE=TEXT VALUE=E NAME=ex$
<BR>5<INPUT TYPE=CHECKBOX Value=5 NAME=check[]><INPUT TYPE=TEXT VALUE=F NAME=ex$
<BR>6<INPUT TYPE=CHECKBOX Value=6 NAME=check[]><INPUT TYPE=TEXT VALUE=G NAME=ex$

<input type=submit>
</FORM>

<?php

include ("./isnum.inc");

echo "<HR>";
echo "<H2> b4 hack </H2>";
echo "0: ", $example[0];
echo "<BR>1: ", $example[1];
echo "<BR>2: ", $example[2];
echo "<BR>3: ", $example[3];
echo "<BR>4: ", $example[4];
echo "<BR>5: ", $example[5];
echo "<BR>6: ", $example[6];
echo "<BR>7: ", $example[7];
echo "<BR>8: ", $example[8];
echo "sizeof example=", sizeof($example);
echo "<HR>\n";

echo "Check size=", sizeof($check), ". ";
                                         
for ($x =0; $x < sizeof($check); $x++)
        {
        echo "unsetting ", $example[$check[$x]], "<BR>\n";
        unset ($example[$check[$x]]);
        }

$example=collapsearray($example);

          
echo "<HR>\n";
echo "<H2>After hack</H2>";
echo "0: ", $example[0];
echo "<BR>1: ", $example[1];
echo "<BR>2: ", $example[2];
echo "<BR>3: ", $example[3];
echo "<BR>4: ", $example[4];
echo "<BR>5: ", $example[5];
echo "<BR>6: ", $example[6];
echo "<BR>7: ", $example[7];
echo "<BR>8: ", $example[8];
echo "sizeof example= ", sizeof($example);

?>

<P> To use this code snippet, paste into a file called test3.php and run.

<P> What you'll find is that the last 1 or two lines of the array passed to the function collapsearray do not get returned properly.

<P> Where you see "See note below!" the following line could
be modified to read
<PRE>
for ($i=0; $i <= sizeof($example)*2; $i++)
</PRE>

And it thereafter works on arrays of large size just fine.

<P> But the reasoning behind this defies any sort of logic!

<P> Compiled: /configure --with-mysql --with-apxs

<P> Apache version 1.3.12

<P> PS: Keep up the good work!

<P> -Benjamin Smith

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>