?
Solved

Doubts about endforeach

Posted on 2009-06-30
3
Medium Priority
?
560 Views
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"){
endforeach;
} 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



0
Comment
Question by:Pedro Chagas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
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 {}.

http://php.net/manual/ru/control-structures.alternative-syntax.php

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

Open in new window

0
 
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:

http://php.net/manual/en/control-structures.alternative-syntax.php
0
 
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 ) {
....
}

becomes

foreach( $a as $b ) :
....
endforeach;

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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

770 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