6

I created 2 simple examples:

First example:

<?php $arr = array(1,2,3,4,5); ?>

<?php foreach ($arr as $element) ?>
<?php { ?>
    <?php echo $element; ?>
<?php } ?>

output:

5 //Is this result wrong?

Second example:

<?php $arr = array(1,2,3,4,5); ?>

<?php foreach ($arr as $element) { ?>
    <?php echo $element; ?>
<?php } ?>

output:

12345

What did I miss about the PHP syntax?

I know that there is an alternative foreach syntax, but in my opinion both shown examples should result in the same output. (Code tested with PHP version: 5.6.12)

Edit:

I know the tags are not needed in every line. To be more precise: I want to know why the two examples give me 2 different results?

Thulur
  • 358
  • 1
  • 4
  • 11
  • I know :D Just wondering about the reason for this output... – Thulur Aug 27 '15 at 12:06
  • just put at the end.....why are you using it in every line.. – Manoj Salvi Aug 27 '15 at 12:06
  • 3
    Just guessing, but perhaps the ?> in the first example is actually being taken as the statement end (loops can be used without braces). At that point, the loop has happened and `$element` is the last value. Then the braces are just take as a code block which you echo, which is 5. – Jonnix Aug 27 '15 at 12:09
  • I read somewhere that some framework actually adds the tags for every line. – Jay Blanchard Aug 27 '15 at 12:13
  • @RiggsFolly I am agree with you but you can't bind to write this in single php tag he can use – Sunil Pachlangia Aug 27 '15 at 12:13
  • @JayBlanchard If you can figure what this framework is/was then please let us know so that we can avoid it at all costs. – MonkeyZeus Aug 27 '15 at 12:14
  • I'll have to go back and look @MonkeyZeus. I basically offered the same correction on an answer sometime back and was fiercely corrected because "that is what the framework does". – Jay Blanchard Aug 27 '15 at 12:17
  • @JonStirling I think you may have put your finger on it – RiggsFolly Aug 27 '15 at 12:18
  • 1
    @JayBlanchard No framework was mentioned in the question. You are safe here, this is a safe place :-) ... for now, muahaha – MonkeyZeus Aug 27 '15 at 12:34

4 Answers4

5

Based on the output, my guess is that:

<?php $arr = array(1,2,3,4,5); ?>

<?php foreach ($arr as $element) ?>
<?php { ?>
    <?php echo $element; ?>
<?php } ?>

is being interpreted as:

<?php
$arr = array(1,2,3,4,5);

foreach ($arr as $element);
{
    echo $element;
}
?>

Looks like a bug in the interpreter? See comments by Rizier123:

  1. Not a bug: stackoverflow.com/q/29284075/3933332
  2. The brackets after the foreach()/Do nothing here/; is just a statement-group: php.net/manual/en/control-structures.intro.php

Anyways, the code looks atrocious with the way you have written it. Please opt for cleaner code.


Reading through the comments under the question I think Jon Stirling explain this symptom the best:

Just guessing, but perhaps the ?> in the first example is actually being taken as the statement end (loops can be used without braces). At that point, the loop has happened and $element is the last value. Then the braces are just take as a code block which you echo, which is 5.

Community
  • 1
  • 1
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • 3
    + The brackets after the `foreach()/*Do nothing here*/;` is just a statement-group: http://php.net/manual/en/control-structures.intro.php – Rizier123 Aug 27 '15 at 12:23
  • I agree with the do nothing, but shouldn't $element be unkown in the curly brackets in that case? – Thulur Aug 27 '15 at 12:30
  • 2
    @Thulur No, 1. The `$element` still exists from the foreach loop and holds the last value of the array. 2. The statement-group doesn't create a new variable scope, means `$element` is well known and also isn't out of scope. – Rizier123 Aug 27 '15 at 12:32
  • @Thulur Try this `` – MonkeyZeus Aug 27 '15 at 12:32
  • So I guess $element is declared outside the scope of the foreach loop? – Thulur Aug 27 '15 at 12:33
  • 2
    @Thulur ^^ Neither the statement group nor the foreach loop creates a new variable scope. Everything in your code is in the same variable scope. – Rizier123 Aug 27 '15 at 12:38
  • @Thulur If you're up for some "light" reading then check out this [question](http://stackoverflow.com/questions/10057671/how-foreach-actually-works) :-) – MonkeyZeus Aug 27 '15 at 12:38
  • @Rizier123 Thanks, I did not know about the scopes till today o.O and now I don't like :D – Thulur Aug 27 '15 at 12:45
  • @Thulur To read more about foreach see: http://php.net/manual/de/control-structures.foreach.php and variable scope (Which isn't the problem in your code): http://stackoverflow.com/q/16959576/3933332 – Rizier123 Aug 27 '15 at 12:47
0

You don't need to use <?php and ?> every single line simply do:

<?php
$arr = array(1,2,3,4,5);

foreach ($arr as $element) {
echo $element;
}
?>

Or alternative syntax:

<?php
$arr = array(1,2,3,4,5);

foreach ($arr as $element)
{
echo $element;
}
?>

Or

<?php
$arr = array(1,2,3,4,5);

foreach ($arr as $element):
echo $element;
endforeach;
?>

When you are doing this:

<?php foreach ($arr as $element) ?>
<?php { ?>
    <?php echo $element; ?>
<?php } ?>

PHP loops nothing because it sees

foreach ($arr as $element)
{
}
echo $element;
Ilanus
  • 6,690
  • 5
  • 13
  • 37
0

Over using <? php> is your problem.

You are completing the foreach context before outputting the result and not doing that in the second.

Look at your examples carefully and you should see what you are doing differently.

jwpfox
  • 5,124
  • 11
  • 45
  • 42
0

I don't know why you use so many php tags, but that's why it doesn't work! try this:

<?php $arr = array(1,2,3,4,5);

foreach ($arr as $element)
{
    echo $element;
} ?>