php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27066 Variable with value 0 is not evaluated correctly within if statement
Submitted: 2004-01-28 02:49 UTC Modified: 2004-01-28 05:40 UTC
From: stitch at tuks dot co dot za Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.4 OS: Windows XP & Linux
Private report: No CVE-ID: None
 [2004-01-28 02:49 UTC] stitch at tuks dot co dot za
Description:
------------
When i use foreach to go through an array, and use an if statement to evaluate the $key, it enters into the if statement incorrectly when $key = 0, even though the if statement reads 'if ($key == "test")'

It is not supposed to enter when $key = 0, but as the Actual result shows, it does. 

This does not happen even when $key = 1, tho. 
I have tested this on both PHP 4.3.4 and PHP 4.0.1

Thank you very much, keep up the good work.

Reproduce code:
---------------
<?
$the_array["aap"] = array("figure"=> "circle", "colour"=>"blue");
$the_array[] = array("figure" => "triangle", "colour"=>"red");
$the_array["test"] = array ("figure" => "sqaure", "colour"=>"neon");

foreach ( $the_array as $key => $value){
	if ($key == "test") {
		echo "\$key = $key\n";
		echo "A ". $value["colour"]. " " . $value["figure"]."\n";
	}
}
?>

Expected result:
----------------
The following output in text:
$key = test
A neon sqaure

Actual result:
--------------
The following output in text:
$key = 0
A red triangle
$key = test
A neon sqaure

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-28 05:40 UTC] mgf@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Use === here (since "test"==0 by design).

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 05:01:29 2024 UTC