Why no error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew G. Koptyaev

    Why no error?

    Why no error on this code?

    <?php
    for ($i=0;$i<5;++$i )
    {
    if ($i==2)
    continue
    print "$i\n";
    }
    ?>

    Output is "2".
    May be true must be "continue;" ?


  • 703designs

    #2
    Re: Why no error?

    That's weird and interesting. You're right, you need the semicolon.

    On Sep 23, 1:20 pm, "Andrew G. Koptyaev" <kopty...@gmail .comwrote:
    Why no error on this code?
    >
    <?php
    for ($i=0;$i<5;++$i )
    {
     if ($i==2)
      continue
     print "$i\n";}
    >
    ?>
    >
    Output is "2".
    May be true must be "continue;" ?

    Comment

    • Michael Fesser

      #3
      Re: Why no error?

      ..oO(703designs )
      >That's weird and interesting.
      It's documented behaviour. The example was taken from the manual and
      describes a situation which you should avoid.

      Maybe it becomes clearer with a little rearrangement, it's still the
      same code:

      <?php
      for ($i = 0; $i < 5; ++$i) {
      if ($i == 2) {
      continue print "$i\n";
      }
      }
      ?>

      Since print is a special language construct and always returns 1, the
      above is equivalent to

      <?php
      for ($i = 0; $i < 5; ++$i) {
      if ($i == 2) {
      print "$i\n";
      continue 1;
      }
      }
      ?>


      >You're right, you need the semicolon.
      Yep.

      Micha

      Comment

      Working...