Php foreach break after 3

Asked 12 years, 4 months ago

Viewed 76k times

Here is the loop.

foreach($results->results as $result){
    echo '
'; echo ''; $text_n = $result->text; echo "
".$text_n."
"; echo '
'; echo "'.$result->from_user.''; $date = $result->created_at; $dateFormat = new DateIntervalFormat(); $time = strtotime($result->created_at); echo "
"; print sprintf('Submitted %s ago', $dateFormat->getInterval($time)); echo '
'; echo "
"; echo "
";

user229044

225k40 gold badges323 silver badges333 bronze badges

asked May 19, 2010 at 12:20

4

With the break command.

You are missing a bracket though.

$i=0;
foreach($results->results as $result){
//whatever you want to do here

$i++;
if($i==3) break;
}

More info about the break command at: http://php.net/manual/en/control-structures.break.php

Update: As Kyle pointed out, if you want to break the loop it's better to use for rather than foreach. Basically you have more control of the flow and you gain readability. Note that you can only do this if the elements in the array are contiguous and indexable (as Colonel Sponsz pointed out)

The code would be:

for($i=0;$i<3;$i++){
$result = $results->results[i];
//whatever you want to do here
}

It's cleaner, it's more bug-proof (the control variables are all inside the for statement), and just reading it you know how many times it will be executed. break / continue should be avoided if possible.

answered May 19, 2010 at 12:23

Php foreach break after 3

pakorepakore

11.1k12 gold badges41 silver badges62 bronze badges

7

  • Declare a variable before the loop, initialize to 0.
  • Increment variable at the beginning of the body of for-each.
  • Check variable at the end of the body of for-each.
    • If it's 3, break.

You have to be careful with this technique because there could be other break/continue in the for-each body, but in your case there isn't, so this would work.

answered May 19, 2010 at 12:22

1

Increment some counter $i at the beggining of the loop and break; when it reaches 3, e.g.:

if ($i++ == 3)
    break;

answered May 19, 2010 at 12:23

Chris SchmichChris Schmich

28.6k5 gold badges74 silver badges94 bronze badges

3

foreach($results->results as $i => $result){ 
   if($i==3) break; 
   //whatever you want to do here 
}

answered May 19, 2010 at 12:54

Php foreach break after 3

Mark BakerMark Baker

206k31 gold badges338 silver badges380 bronze badges

3

Can I use break in forEach PHP?

To terminate the control from any loop we need to use break keyword. The break keyword is used to end the execution of current for, foreach, while, do-while or switch structure.

How do you exit a forEach loop?

The forEach() function respects changes to the array's length property. So you can force forEach() to break out of the loop early by overwriting the array's length property as shown below.

Does PHP return stop a loop?

PHP 7 requires a return . A break; is not needed because the loop ends on return . A break; is usually used in a switch or loop whenever you have found your needed item.

How do you end a loop after one iteration?

The break statement exits a for or while loop completely. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement..
break is not defined outside a for or while loop. To exit a function, use return ..