php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #4453 Array of widgets problem when array is zero based
Submitted: 2000-05-15 11:44 UTC Modified: 2000-07-25 23:38 UTC
From: redbadge at mindspring dot com Assigned:
Status: Closed Package: Other
PHP Version: 4.0 Release Candidate 2 OS: Linux RedHat 6.1 (2.2.12-20)
Private report: No CVE-ID: None
 [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>";

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-07-25 23:38 UTC] zak@php.net
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.



 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 21:01:29 2024 UTC