Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

break

break ends execution of the current for, foreach, while, do-while or switch structure.

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

<?php
$arr
= array('one', 'two', 'three', 'four', 'stop', 'five');
while (list(,
$val) = each($arr)) {
    if (
$val == 'stop') {
        break;   
/* You could also write 'break 1;' here. */
   
}
    echo
"$val<br />\n";
}

/* Using the optional argument. */

$i = 0;
while (++
$i) {
    switch (
$i) {
    case
5:
        echo
"At 5<br />\n";
        break
1/* Exit only the switch. */
   
case 10:
        echo
"At 10; quitting<br />\n";
        break
2/* Exit the switch and the while. */
   
default:
        break;
    }
}
?>



continue> <foreach
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
break
Gautam
22-Aug-2007 02:53
<?php
/*
break :break command exits the innermost loop construct which contains it.
break ends execution of the current for, foreach, while, do-while or switch structure.
break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.

You can view various output by changing comparision operator(<,==,>) or value of $limit
*/
$to_square_root=65536;
$i=1;
$limit=4;
while (
true) {
$square_root=sqrt($to_square_root);
echo
"Square Root of $to_square_root is $square_root.<BR>";
$to_square_root=$square_root;
$i=$i+1;
if (
$i>$limit) // if ($i<$limit) is used, loop breaks on very first execution
break;
}
$loop=$i-1;
echo
"This loop is executes for $loop times.";
/* Above codes produces following output in browser
Square Root of 65536 is 256
Square Root of 256 is 16
Square Root of 16 is 4
Square Root of 4 is 2
This loop is executes for 4 times
*/
?>
pinkgothic at gmail dot com
22-Jun-2007 04:10
To add to the responses given to "vlad at vlad dot neosurge dot net" - I'd like to note the lack of automatic breaking in 'default' can be a very good thing. Consider this useful snippet:

<?php

switch((string) $_REQUEST['mode']) {
  default:
    
$_REQUEST['mode'] = "search";
    
// fall through...
 
case 'search' :
  case
'list' :
  case
'add' :
  case
'edit' :
     require(
dirname(__FILE__)."/incs/".$_REQUEST['mode'].".php");
     break;
}

?>

I personally find that far easier to look at than, for example:

<?php

$valid_modes
= array('search','list','add','edit');
if (
in_array($_REQUEST['mode'],$valid_modes)) {
     require(
dirname(__FILE__)."/incs/".$_REQUEST['mode'].".php");
} else {
     require(
dirname(__FILE__)."/incs/search.php");
}

?>

...and it even has the added benefit that the switch() variant only has one require() statement, which makes for easier maintenance, e.g. if the directory changes, or what-have-you.

(Consider the above pseudocode, please, it's not tested - it's code illustrating a point only.)
vinyanov at poczta dot onet dot pl
05-May-2007 05:36
Note that the break argument accepts any expression, including a function result. So you may want to dynamically choose the loop level to break from:

<?php

// the print() function returns 1

function icarus()
{
    while(print(
'sea level, '))
        while(print(
'through the clouds, '))
            while(print(
'close the Sun - '))
                break
rand(print('FEATHERS LOSS! - '), 3);
   
    print(
'no feathers remaining.');
}

icarus();

?>
Jonny Arnold
23-Feb-2007 09:55
Note that break also works with the foreach loop.
18-Jan-2007 06:12
If you wonder how to end execution of a function (as I did), it's that simple: return

function foo($a) {
 if(!$a) return;
 echo 'true';
 // some other code
}

foo(true) will echo 'true', foo(false) won't echo anything (as return ends execution of the function. Of course, therefore there is no need for 'else' before 'echo').
clean_code at is_good_code dot com
08-Nov-2006 12:07
"Just an insignificant side not: Like in C/C++, it's not necessary to break out of the default part of a switch statement in PHP."

--Yes it is, it's just that traditionally default: is the last entry of a switch and so nothing happens after.

-If it was, for whatever reason, not the last entry the script would bawk, there is no implicit break; associated with switch.
traxer at gmx dot net
30-Dec-2005 06:53
vlad at vlad dot neosurge dot net wrote on 04-Jan-2003 04:21

> Just an insignificant side not: Like in C/C++, it's not
> necessary to break out of the default part of a switch
> statement in PHP.

It's not necessary to break out of any case of a switch  statement in PHP, but if you want only one case to be executed, you have do break out of it (even out of the default case).

Consider this:

<?php
$a
= 'Apple';
switch (
$a) {
    default:
        echo
'$a is not an orange<br>';
    case
'Orange':
        echo
'$a is an orange';
}
?>

This prints (in PHP 5.0.4 on MS-Windows):
$a is not an orange
$a is an orange

Note that the PHP documentation does not state the default part must be the last case statement.
develop at jjkiers dot nl
13-Jul-2005 02:06
To php_manual at pfiff-media dot de:

This is only possible with PHP5, because <PHP5 doesn't have a proper exception infrastructure.
php_manual at pfiff-media dot de
13-Mar-2005 06:21
instead of using the while (1) loop with break like suggested in an earlier note, I would rather recommend throwing an exception, e.g.:

<?php
try {
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com
Z058440144362 Z348613067571