why it use this ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

why it use this ?

Post by everydayrun »

Code: Select all

<body>
<h1>News Articles</h1>
<?php foreach ($articles as $row) { ?>
<h2><?php echo $row->headline ?></h2>
<p>
<?php echo $row->body ?>
</p>
<?php } ?>
</body>
why it use " { ?>" and "<?php } ?>"? i feel the "{" is surplus.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: why it use this ?

Post by Weirdan »

That's a part of foreach syntax for when the loop body includes more than one statement. Using alternative syntax in templates make more sense IMO (and if you have short_tag enabled it's even more sensible):

Code: Select all

<body>
<h1>News Articles</h1>
<? foreach ($articles as $row) : ?>
<h2><?= $row->headline ?></h2>
<p><?= $row->body ?></p>
<? endforeach; ?>
</body>
Post Reply