Doubts about endforeach

Posted on 2009-06-30
Medium Priority
Last Modified: 2012-05-07
Hi E's, I use foreach for do cycles with arrays, and I want do like a break in foreach, in this case I discover endforeach in php.net.
My doubt about endforeach is what happening when I call endforeach, they finish all the cycle or just the key in that moment?

In practice if I have this array and this code:
1 => um
2 => dois
3 => tres

foreach($array as $element){
if ($element == "um"){
} else {
echo "do something";

My doubt is if the system run array [2] and [3] after endforeach (finish all cycle foreach or just for the moment that endforeach as call)?

Regards, JC

Question by:Pedro Chagas
  • 2
LVL 39

Accepted Solution

Roger Baklund earned 400 total points
ID: 24750820
That is not how to use endforeach. This is not a function, it is an alternative way to end a foreach loop block instead of using {}.


What you need is "break;", like this:
foreach($array as $element){
  if ($element == "um")
  echo "do something";
# if you want to skip "um" but continue looping, use "continue;" instead:
foreach($array as $element){
  if ($element == "um")
  echo "do something";

Open in new window

LVL 39

Expert Comment

by:Roger Baklund
ID: 24750828
Heh, that link was to the russian version of the php manual, but the text on that page is currently in english... for future reference, this is the english version:

LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 100 total points
ID: 24752576
One thing that should be pointed out, because it is easily missed in the documentation, is that the OPENING brace is replaced with a colon so that

foreach( $a as $b ) {


foreach( $a as $b ) :

Also note that "endforeach" is followed with a ; whereas a } does not  have a following ;


Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question