1
<?php if (count($errors) > 0): ?>
  <div class="error">
     <?php foreach ($errors as $error) ?>
     <p><?php echo $error; ?></p>
  <?php endforeach ?>
  </div>
<?php endif ?>

My apologies if this is obvious to some of you more experienced PHP developers, but my code has the following errors;

ForEach must uses braces

Unexpected endforeach after string

I know that these { braces should inserted somewhere, but I'm not sure where and in what order? Also, what do I need to do after the string in question?

Any help would be much appreciated. Thanks.

Community
  • 1
  • 1
Skin99
  • 91
  • 1
  • 1
  • 8

1 Answers1

4

If you're using alternative syntax, you need to add a colon : after the foreach

foreach ($errors as $error):
                          ^^^

Alternatively, you can use brackets {} (which I personally find easier to read):

<?php foreach ($errors as $error) { ?>
     <p><?php echo $error; ?></p>
<?php } ?>

And there's no reason to go in and out of PHP like that, you might as well do it all in one go, using double-quotes we can still pass variables by value, so we don't need to concat your HTML to the $error variable either.

<?php foreach ($errors as $error):
    echo "<p>$error</p>";
endforeach; ?>
Qirel
  • 25,449
  • 7
  • 45
  • 62