note 72647 deleted from control-structures.foreach by googleguy
| From: | googleguy@php.net | Date: | Wed, 14 Nov 2012 22:32:24 +0000 |
| Subject: | note 72647 deleted from control-structures.foreach by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191974@lists.php.net to get a copy of this message | ||
Note Submitter:
----
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.