Reputation: 465
I am learning PHP from the book "Murach PHP and MySQL 2nd Edition" and I came across this code which I don't fully understand...
<select name="productkey">
<?php foreach ($products as $key => $product):
$cost = number_format($product['cost'], 2);
$name = $product['name'];
$item = $name . ' ($)' . $cost . ')';
?>
<option value="<?php echo $key; ?>"> <?php echo $item; ?></option>
<?php endforeach; ?>
</select>
Why are there instructions after the colon (:) at the start of the foreach loop??
I read that after the colon there should be "?>" and then the instructions to execute and then at end "" to mark the end of the foreach loop...
Please explain this AND IF I WRITE THE LINES STARTING WITH $cost,$name and $item after <?php foreach ($products as $key => $product): ?>
would the code still work???
Upvotes: 0
Views: 14488
Reputation: 75
As you said it normally called PHP alternate syntax for control structure. You can read in more depth in that article:
Alternate Syntax is normally used when we need to mix-up HTML inside PHP. In your code:
<select name="productkey">
<?php foreach ($products as $key => $product):
$cost = number_format($product['cost'], 2);
$name = $product['name'];
$item = $name . ' ($)' . $cost . ')';
?>
<option value="<?php echo $key; ?>"> <?php echo $item; ?></option>
<?php endforeach; ?>
You can see you have a option inside foreach. In this type of case it's better to use alternate syntax. We can use the alternate syntax for if else and switch case as well.
Upvotes: 1
Reputation: 21661
The alternative syntax is mainly for use with mixed html content. The main advantage is say you have something like this
<?php if( $var ){ ?>
<!-- some html here -->
<?php foreach( $var as $v ){ ?>
<!-- some html here -->
<?php if( $v=='foo'){ ?>
<!-- some html here -->
<?php } ?>
<!-- some html here -->
<?php } ?>
<!-- some html here -->
<?php } ?>
You quickly wind up with a mess of }
everywhere, compare to this
<?php if( $var ): ?>
<!-- some html here -->
<?php foreach( $var as $v ): ?>
<!-- some html here -->
<?php if( $v=='foo'): ?>
<!-- some html here -->
<?php endif; ?>
<!-- some html here -->
<?php endforeach; ?>
<!-- some html here -->
<?php endif; ?>
This is a small example so I don't think if fully shows the issue, but it's much easier to where the if
blocks end and the foreach
ends even in this.
Now multiple that by about 200 lines of mixed content. Otherwise you can look at it as the :
is a {
and the end{blocktype};
is the }
Personally I never use it ( except on SO ), because I don't mix HTML and PHP in my code.
cheers.
Upvotes: 1
Reputation: 8748
:
called an Alternative Syntax For Control Structures.
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.
It allows you to omit braces {} from a foreach to make it look cleaner and its mostly used, within templates.
Upvotes: 3
Reputation: 517
The colon is an alternative to using brackets. This:
foreach (...) :
#execute Code in loop
endforeach;
is the same as this:
foreach (...) {
#execute Code in loop
}
Upvotes: 1
Reputation: 674
yes it would still work : is the same as { but you have to write endforeach at the end of your foreach it does the same thing more info check out this page documentation
Upvotes: 4