redarrow Posted December 5, 2008 Share Posted December 5, 2008 advance thank you....... Can you give me a proper example, why we need the brake and continue command for the, for loop please.... understand break and continue but when do we use it or what for thank you......... <?php // ((continue)) forces the code to carry on looping, and show's the result minus the if condition......... $a=array("john","paul","petter","lucke"); for($b=0; $b<count($a); $b++){ if($a[$b]=="petter"){ continue; } echo " <br> continued minus condition $a[$b] <br>"; } ?> <?php // ((break)) forces the loop to stop, and only shows the remaining echoed condition before the break......... $a=array("john","paul","petter","lucke"); for($b=0; $b<count($a); $b++){ if($a[$b]=="petter"){ break; } echo " <br> braked minus condition $a[$b] <br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/ Share on other sites More sharing options...
Mchl Posted December 5, 2008 Share Posted December 5, 2008 Use continue if you want to skip one iteration of the loop. So when $a[$b]=="petter" one iteration of loop will be skipped, but the loop will run efterwards. Use break, if you want to stop the loop. When $a[$b]=="petter" then the loop stops and script execution restarts from the first line after it. Real life examples: Consider a query that returns result set as this SKIP, 1 foo, 2 bar, 3 SKIP, 4 baz, 5 STOP, 6 bazbar, 7 while($row = mysql_fetch_array($result)) { if ($row[0] == "SKIP") continue; if ($row[0] == "STOP") break; echo $row[1]."<br\>/n"; } This will echo 2 3 5 Link to comment https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/#findComment-706596 Share on other sites More sharing options...
redarrow Posted December 5, 2008 Author Share Posted December 5, 2008 This will echo 2 3 5 what about 7 that should be echoed shouldn't it.... no sorry the brake stops it at 5 yee i see Link to comment https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/#findComment-706615 Share on other sites More sharing options...
redarrow Posted December 5, 2008 Author Share Posted December 5, 2008 What reason it it used for, your example grate but why we need it......... all i can see is it takes something away from a set condition or brake from a set condition...... Link to comment https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/#findComment-706617 Share on other sites More sharing options...
redarrow Posted December 5, 2008 Author Share Posted December 5, 2008 output <br> paul <br> <br> lucke <br> <br> ron <br> <?php $a=array("john","paul","petter","lucke","ron"); for($b=0; $b<count($a); $b++){ if($a[$b]=="john"){continue;} if($a[$b]=="petter"){continue;} if($a[$b]=="luke"){break;} echo " <br> $a[$b] <br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/#findComment-706621 Share on other sites More sharing options...
Mchl Posted December 5, 2008 Share Posted December 5, 2008 continue is commonly used to filter results, that are not needed. For example: I have a textarea form into which some data from Excel is pasted, and then posted to PHP In php I analyze the data line by line. If there's an empty line, I just skip it. break is used to stop processing, when for example a value has been found, or the result of calculation fits your requirements In the example with the textarea before: I could have stop processing using break, when 10 non-empty lines were processed. The pseudo code would look like this $data = $_POST['textarea']; $lines = explode("\n",$data); $count = 0; foreach ($lines as $key => $line) { if(empty($line)) continue; //the line is empty, nothing to do, go to next line process_line($line); // a function that does something with line $count++; //increase number of lines processed if ($count >= 10) break; //stop the loop after 10 processed lines } Link to comment https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/#findComment-706623 Share on other sites More sharing options...
redarrow Posted December 5, 2008 Author Share Posted December 5, 2008 so u can use (( break and continue )) with any loop statement only... foreach() while() for() Link to comment https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/#findComment-706626 Share on other sites More sharing options...
Mchl Posted December 5, 2008 Share Posted December 5, 2008 Also switch (probably the most common use of break) Link to comment https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/#findComment-706630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.