note 72647 added to control-structures.foreach

From: Date: Sat, 27 Jan 2007 18:50:47 +0000
Subject: note 72647 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-122274@lists.php.net to get a copy of this message
Here is an obvious question to most of the readers, but it took me about two precious minutes to figure out, so I figured I will share it will you: What will be the output of the following statement: <?php $data = array('1' => 'field1', '2' => 'field2'); foreach ($data as $field_index => $field_name); { echo "$field_name"; } ?> Correct answer is 'field2', and not 'field1field2'. The forgotten semicolon at the foreach line does not trigger a syntax error, but php treats it as an empty statement.. and then the block is run once with the last value set into $field_name. ---- Server IP: 69.147.83.197 Probable Submitter: 89.138.77.16 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/72647 Del: integrated -- https://master.php.net/note/delete/72647/integrated Del: useless -- https://master.php.net/note/delete/72647/useless Del: bad code -- https://master.php.net/note/delete/72647/bad+code Del: spam -- https://master.php.net/note/delete/72647/spam Del: non-english -- https://master.php.net/note/delete/72647/non-english Del: in docs -- https://master.php.net/note/delete/72647/in+docs Del: other reasons-- https://master.php.net/note/delete/72647 Reject -- https://master.php.net/note/reject/72647 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#122274) next »