Bug #51829 [Bgs]: Array output unexpected result

From: Date: Sun, 16 May 2010 04:34:55 +0000
Subject: Bug #51829 [Bgs]: Array output unexpected result
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-150490@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=51829&edit=1

 ID:               51829
 User updated by:  josefrancklin at yahoo dot com dot br
 Reported by:      josefrancklin at yahoo dot com dot br
 Summary:          Array output unexpected result
 Status:           Bogus
 Type:             Bug
 Package:          Arrays related
 Operating System: Windows XP
 PHP Version:      5.3.2

 New Comment:

//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)
-.-


Previous Comments:
------------------------------------------------------------------------
[2010-05-14 21:44:19] 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-14 20:09:16] 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




------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51829&edit=1


Thread (4 messages)

« previous php.bugs (#150490) next »