From:             drphilosopher at yahoo dot com
Operating system: Windows XP Service Patch 2
PHP version:      5.0.3
PHP Bug Type:     Arrays related
Bug description:  String '0' not handled correctly in foreach

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 bug report at http://bugs.php.net/?id=33368&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=33368&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=33368&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=33368&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=33368&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=33368&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=33368&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=33368&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=33368&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=33368&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=33368&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=33368&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=33368&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=33368&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=33368&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=33368&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=33368&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=33368&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=33368&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=33368&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=33368&r=mysqlcfg

Reply via email to