php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51829 Array output unexpected result
Submitted: 2010-05-14 20:09 UTC Modified: 2010-05-16 20:21 UTC
From: josefrancklin at yahoo dot com dot br Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.2 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: josefrancklin at yahoo dot com dot br
New email:
PHP Version: OS:

 

 [2010-05-14 20:09 UTC] josefrancklin at yahoo dot com dot br
Description:
------------
Code typed below generates unexpected result in the [0] term from an array.

Test script:
---------------
for ($i=0;$i<3;$i++)
{
	$array_id=array( 0 => 0 , 1 => 1 , 2 => 2 );
	$array_id[$i]="x";
	for ($j=0;$j<3;$j++)
	{
		print "term [$j] is $array_id[$j]";
		if ($array_id[$j]!=$j)
			print ", not $j";
		print "<BR>\n";
	}
	print "<BR>\n";
}


Expected result:
----------------
term [0] is x, not 0
term [1] is 1
term [2] is 2

term [0] is 0
term [1] is x, not 1
term [2] is 2

term [0] is 0
term [1] is 1
term [2] is x, not 2



Actual result:
--------------
array[0] is x
array[1] is 1
array[2] is 2

array[0] is 0
array[1] is x, not 1
array[2] is 2

array[0] is 0
array[1] is 1
array[2] is x, not 2



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-14 21:44 UTC] dtajchreber@php.net
-Status: Open +Status: Bogus
 [2010-05-14 21:44 UTC] dtajchreber@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

You have discovered type juggling. See: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

'x' turns into 0. 0 == 0. Your if statement is never executed.
 [2010-05-16 06:34 UTC] josefrancklin at yahoo dot com dot br
//for ($i=0 ; $i<3 ; $i++){

// first loop: $i is 0;

	$array_id = array( 0 => 0 , 1 => 1 , 2 => 2 );
	$array_id[$i] = "x";  // $i is 0, then $array_id[0] = "x"
//	print_r($array_id) here results Array ( [0] => x [1] => 1 [2] => 2 )
	for ($j=0 ; $j<3 ; $j++)
	{
		if ($array_id[$j] != $j) // $j is 0, then if ($array_id[0] != 0)
			print ", not $j"; // $array_id[0] is "x" (!=0), but nothing printed
//((problem, how to solve???))
	}


// second loop: $i is 1;

	$array_id = array( 0 => 0 , 1 => 1 , 2 => 2 );
	$array_id[$i] = "x";  // $i is 1, then $array_id[1] = "x"
//	print_r($array_id) here results Array ( [0] => 0 [1] => x [2] => 2 )
	for ($j=0; $j<3; $j++)
	{ // lets go to $j=1...
		if ($array_id[$j] != $j) // $j is 1, then if ($array_id[1] != 1)
			print ", not $j"; // $array_id[1] is "x" (!=1), is's printed ((ok))
	}
//}

Problem... I've tried several forms to change the code in order to work correct but no success... I think I don't know programming... (and write in english too)
-.-
 [2010-05-16 20:22 UTC] dtajchreber@php.net
In your original code, change:
    if ($array_id[$j]!=$j)
to be:
    if ($array_id[$j]!==$j)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Jun 01 20:01:24 2020 UTC