ID:               33368
 Updated by:       [EMAIL PROTECTED]
 Reported By:      drphilosopher at yahoo dot com
-Status:           Feedback
+Status:           Bogus
 Bug Type:         Arrays related
 Operating System: Windows XP Service Patch 2
 PHP Version:      5.0.3
 New Comment:

No bug here, numeric strings in array indices have always been
converted to integers.  So you are effectively doing 0=='NOLIMIT' and
the integer representation of 'NOLIMIT' is 0.  You can use a === there
to get what you want, or you can explicitly cast the $key to a string
in the comparison.

  $hi = ((string)$key == 'NOLIMIT' ? 'hi' : '');



Previous Comments:
------------------------------------------------------------------------

[2005-06-16 21:12:30] [EMAIL PROTECTED]

Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip



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

[2005-06-16 20:35:05] drphilosopher at yahoo dot com

Description:
------------
The string, '0', is not handled correctly 
in a foreach loop (and possibly elsewhere).

The first loop executes correctly.  The 
second and third loops perform incorrect
comparisons.  

In loops two and three, '0' == 'NOLIMIT'
evaluates to true when it should be false.


Reproduce code:
---------------
$arr = array('0' => '$0',
             '1000' => '$1,000', 
             '2000' => '$2,000', 
             '3000' => '$3,000', 
             'NOLIMIT' => 'NO LIMIT');
foreach ($arr as $key => $value)
{
    echo "$key : $value $hi\n";
}
echo "\n";
foreach ($arr as $key => $value)
{
    if ($key == 'NOLIMIT') { continue; }
    echo "$key : $value $hi\n";
}
echo "\n";
foreach ($arr as $key => $value)
{
    $hi = ($key == 'NOLIMIT' ? 'hi' : '');
    echo "$key : $value      $hi\n";
}

Expected result:
----------------
0 : $0 
1000 : $1,000 
2000 : $2,000 
3000 : $3,000 
NOLIMIT : NO LIMIT 

0 : $0 
1000 : $1,000 
2000 : $2,000 
3000 : $3,000 

0 : $0
1000 : $1,000      
2000 : $2,000      
3000 : $3,000      
NOLIMIT : NO LIMIT      hi

Actual result:
--------------
0 : $0 
1000 : $1,000 
2000 : $2,000 
3000 : $3,000 
NOLIMIT : NO LIMIT 

1000 : $1,000 
2000 : $2,000 
3000 : $3,000 

0 : $0      hi
1000 : $1,000      
2000 : $2,000      
3000 : $3,000      
NOLIMIT : NO LIMIT      hi

I am using PHP 5.03 and Apache 2.0.53 on Windows XP, service patch 2.


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


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

Reply via email to