Okay I am officially confused.
I’m reading up on this MVC intro in php and I see this code, and I added some items into array on top to see if it really works.
<?php
$members = array('apple', 'oranges', 'banana');
?>
<html>
<h1>Members of community.com:</h1>
<ul>
<?php foreach ($members as $i => $member) : ?>
<li>Member #<?php echo $i + 1; ?>: <?php echo $member; ?></li>
<?php endforeach; ?>
</ul>
</html>
I notice there’s a : in the line of the foreach statement. Where does this come from? More importantly, what is it? Is this : symbol mean “okay we will continue this statement in the next line” ?
But besides that, this is cool trick I learned. Less html tags inside my php echo’s I guess.
let me know what you think, thanks!
This is an "Alternative syntax for control structures"… see https://www.php.net/manual/en/control-structures.alternative-syntax.php and http://www.php.net/manual/en/control-structures.foreach.php#82511.
It is mostly used in view code that designers might be looking at because it is believed to be easier to understand for non-programmers. I highly recommend against using it as it really offers nothing over a commented set of braces and many IDEs don’t play nicely with it. If your code may ever need to be viewed by others it is best to code without using alternate syntax.
is better represented as…
After you start nesting multiple sets structures that end with endstructure; instead of a brace then commented braces give a more detailed description of which block is being ended. You can, of course, comment the endstructure; syntax but you still have the problem that many IDEs won’t be able to match them for you.