note 118028 added to control-structures.foreach
| From: | pnc at balintx dot me | Date: | Mon, 21 Sep 2015 10:14:44 +0000 |
| Subject: | note 118028 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203827@lists.php.net to get a copy of this message | ||
Just a simple strange behavior I have ran into:
If you accidentally put a semicolon after the foreach statement, you get no errors, but the loop
will only run on the last element of the array:
<?php
$array = array(1,2,3);
foreach ($array as $key);
{
echo $key;
}
// output: 3
?>
Correctly:
<?php
$array = array(1,2,3);
foreach ($array as $key)
{
echo $key;
}
// output: 123
?>
It took me a while to find that semicolon.
----
Server IP: 72.52.91.14
Probable Submitter: 80.99.21.84
----
Manual Page -- http://php.net/manual/en/control-structures.foreach.php
Edit -- https://master.php.net/note/edit/118028
Del: integrated -- https://master.php.net/note/delete/118028/integrated
Del: useless -- https://master.php.net/note/delete/118028/useless
Del: bad code -- https://master.php.net/note/delete/118028/bad+code
Del: spam -- https://master.php.net/note/delete/118028/spam
Del: non-english -- https://master.php.net/note/delete/118028/non-english
Del: in docs -- https://master.php.net/note/delete/118028/in+docs
Del: other reasons-- https://master.php.net/note/delete/118028
Reject -- https://master.php.net/note/reject/118028
Search -- https://master.php.net/manage/user-notes.php