note 83711 added to control-structures.foreach

From: Date: Sun, 08 Jun 2008 20:10:25 +0000
Subject: note 83711 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-140249@lists.php.net to get a copy of this message
In PHP 4, foreach is broken when used on an undefined array element (see http://bugs.php.net/bug.php?id=41169 ): <?php $example = array(); foreach ( $example[ 'element' ] as $value ) { } var_dump( $example ); ?> Expected output: array(0) { } Actual output: array(1) { ["element"]=> NULL } I think this is probably the least painful workaround: <?php $example = array(); foreach ( (array) $example[ 'element' ] as $value ) { } var_dump( $example ); ?> Output: array(0) { } ---- Server IP: 69.147.83.197 Probable Submitter: 71.255.133.253 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/83711 Del: integrated -- https://master.php.net/note/delete/83711/integrated Del: useless -- https://master.php.net/note/delete/83711/useless Del: bad code -- https://master.php.net/note/delete/83711/bad+code Del: spam -- https://master.php.net/note/delete/83711/spam Del: non-english -- https://master.php.net/note/delete/83711/non-english Del: in docs -- https://master.php.net/note/delete/83711/in+docs Del: other reasons-- https://master.php.net/note/delete/83711 Reject -- https://master.php.net/note/reject/83711 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140249) next »