|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-05-15 11:44 UTC] redbadge at mindspring dot com
I have recreated this problem in both PHP 3.0.16 and 4.0 RC2. When using an array of checkboxes that had a zero based value, the following script had variable and erratic results. When the commented out for{} construct that start at 1 are added back and comment out the for{} that uses 0, it works fine. After reviewing the Open Bug Report list, I have found that Bug ID #4433 may be having the same problem.
Script to recreate problem:
----------------------------
<?php
echo "<form action='$PHP_SELF' method='post'>\n";
$NumChecks = count($chkArray);
echo "The number of Checks in the array is: $NumChecks<BR>\n";
if ($NumChecks > 0)
{
for ($y=0; $y<$NumChecks; ++$y)
//for ($y=1; $y<=10; ++$y)
{
echo "\$chkArray[$y] is $chkArray[$y]<BR>\n";
}
}
else
{
echo "\$chkArray is empty<BR>\n";
}
$NumHide = count($HideArray);
echo "The number of Hidden items in the array is: $NumHide<BR>\n";
If ($NumHide > 0)
{
for ($y=0; $y<$NumHide; ++$y)
//for ($y=1; $y<=$NumHide; ++$y)
{
echo "\$HideArray[$y] is $HideArray[$y] <br>\n";
}
}
else
{
echo "\$HideArray is empty<BR>\n";
}
//for( $x=1; $x<=10; ++$x)
for( $x=0; $x<10; ++$x)
{
$strChecked = "";
if ( $chkArray[$x] == $x ) { $strChecked = " checked "; }
$HideVal = $x+100;
echo "<input type='checkbox' name='chkArray[$x]' value=$x".$strChecked."><br>\n";
echo "<input type='hidden' name='HideArray[$x]' value=$HideVal>\n";
}
echo "<input type='submit' name='GetData' value='Submit'>";
echo "</form>";
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 08:00:01 2025 UTC |
This is a problem with the logic for the program, not with PHP. PHP considers "" and 0 to be equivalent when using the == operator. In PHP 4 you can use the === operator to ensure that the values being compared match in type and in value. Note that data posted via a form is considered to be a string, so for the equivalency test I place $x in quotes to cast it to a string. Change this line: if ( $chkArray[$x] == $x ) { $strChecked = " checked "; } to this: if ($chkArray[$x] === "$x") {$strChecked = " checked";} and your script should work as anticipated.