php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47609 foreach() fails to compare key properly
Submitted: 2009-03-10 00:01 UTC Modified: 2009-03-10 00:22 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: sezer@php.net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2CVS-2009-03-09 (snap) OS: RHEL 5.3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sezer@php.net
New email:
PHP Version: OS:

 

 [2009-03-10 00:01 UTC] sezer@php.net
Description:
------------
Following script will print '4-FAIL' using php 5.2.9 build and today's snapshot php5.2-200903092130.

Following returns true where $k = int(0), which is wrong:
if ($k == 'abc')
	echo "3-FAIL\n";

Also confirmed this is failing on 5.1.6.


Reproduce code:
---------------
<?php

$a = array(4);
if ($a[0] != '4')
	echo "1-FAIL";

if ($a[0] != 4)
	echo "2-FAIL";

if ($a[0] == 'abc')
	echo "3-FAIL\n";

foreach ($a as $k => $v)
{
	// $k is ONLY and ALWAYS ZERO (0)
	var_dump($k);
	var_dump($v);

	if ($k == 'abc')
		echo "4-FAIL\n";

	if ($k === 'abc')
		echo "5-FAIL\n";

	if ($v == 'abc')
		echo "6-FAIL\n";
}


Expected result:
----------------
Code should not print anything.
Only key is int(0) and only value is int(4)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-10 00:22 UTC] kalle@php.net
Expected result, see the first example on the comparison operators page:
http://www.php.net/manual/en/language.operators.comparison.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 05:01:33 2025 UTC