#19943 [Ver->Ctl]: Ragged array returns undefined results

From: Date: Mon, 28 Oct 2002 02:08:18 +0000
Subject: #19943 [Ver->Ctl]: Ragged array returns undefined results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-23512@lists.php.net to get a copy of this message
ID: 19943 Updated by: sterling@php.net Reported By: su-php@bossi.com -Status: Verified +Status: Critical Bug Type: Arrays related Operating System: Win2K PHP Version: 4.3.0-dev Previous Comments: ------------------------------------------------------------------------ [2002-10-16 22:51:14] iliaa@php.net A pretty interesting bug this is, there appear to be 2 possible behaviours that can happen here and only 1 is correct. <?php $ar = array(); for ( $count = 0; $count < 10; $count++ ) { $ar[$count] = "$count"; $ar[$count]['idx'] = "$count"; } for ( $count = 0; $count < 10; $count++ ) { echo $ar[$count]." -- ".$ar[$count]['idx']."\n"; } ?> The code above will output: t 0 -- t t 1 -- t t 2 -- t t 3 -- t t 4 -- t t 5 -- t t 6 -- t t 7 -- t t 8 -- t t 9 -- t /home/rei/PHP_CVS/php4/Zend/zend_operators.c(1008) : Freeing 0x08369384 (6 bytes), script=a.php Last leak repeated 9 times If the " around the $count variable are removed then the script reports: Warning: Cannot use a scalar value as an array in /home/rei/PHP_CVS/php4/a.php on line 7 /home/rei/PHP_CVS/php4/a.php(7) : Warning - Cannot use a scalar value as an array for every assignment and does no initialize any of the $ar[$count]['idx'] values. ------------------------------------------------------------------------ [2002-10-16 19:54:16] su-php@bossi.com However, the following codes does work, even though the array is still ragged: <? $ragged = array(); for ( $count = 0; $count < 10; $count++ ) { $ragged['idx'][$count] = 'ragged '.$count; $ragged[$count] = 'single '.$count; } ?> <html><head></head><body> <table border="1"> <tr> <td>Expected</td><td>Actual</td> <td>Expected IDX</td><td>Actual IDX</td> </tr> <? for ( $count = 0; $count < 10; $count++ ) { ?> <tr> <td> <?= 'single '.$count ?> </td><td> <?= $ragged[$count] ?> </td> <td> <?= 'ragged '.$count ?> </td><td> <?= $ragged['idx'][$count] ?> </td> </tr> <? } ?> </table></body></html> ------------------------------------------------------------------------ [2002-10-16 19:50:57] su-php@bossi.com When using an array that has ragged indices, the value in the array is undefined. Well, actually it can be defined, but it is unexpected. No errors or warnings are reported. Sample Code: <? $ragged = array(); for ( $count = 0; $count < 10; $count++ ) { $ragged[$count] = 'single '.$count; $ragged[$count]['idx'] = 'ragged '.$count; } ?> <html><head></head><body> <table border="1"> <tr> <td>Expected</td><td>Actual</td> <td>Expected IDX</td><td>Actual IDX</td> </tr> <? for ( $count = 0; $count < 10; $count++ ) { ?> <tr> <td> <?= 'single '.$count ?> </td><td> <?= $ragged[$count] ?> </td> <td> <?= 'ragged '.$count ?> </td><td> <?= $ragged[$count]['idx'] ?> </td> </tr> <? } ?> </table></body></html> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=19943&edit=1

« previous php.bugs (#23512) next »