Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this Colon inside foreach statement for php?

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!

like image 514
Matt Avatar asked Jul 04 '11 04:07

Matt


People also ask

What is foreach statement in PHP?

The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

What is key in foreach?

In every loop the current element's key is assigned to $key and the internal array pointer is advanced by one and the process continue to reach the last array element. Example -1 : In the following example, we define an array with five elements and later we use foreach to access array element's value.

Can we use foreach inside foreach in PHP?

Due to operator precedence, you cannot put braces around the inner foreach loop. This is structured very much like the nested for loop. The outer foreach is iterating over the values in bvec , passing them to the inner foreach , which iterates over the values in avec for each value of bvec .

What is PHP alternative syntax?

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.


2 Answers

This is an "Alternative syntax for control structures"... see http://ru2.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.

foreach ($setOfItems as $item):
    //do something
endforeach;

is better represented as...

foreach ($setOfItems as $item) {
    //do something
} // end ($setOfItems as $item) foreach

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.

like image 156
Night Owl Avatar answered Sep 26 '22 04:09

Night Owl


This means that the foreach will continue until it reaches endforeach. It is an alternate syntax to {}.

See http://www.php.net/manual/en/control-structures.foreach.php#82511

like image 44
Brad Avatar answered Sep 25 '22 04:09

Brad