为什么“无法打破/继续 1 级”出现在 PHP 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11988281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:34:18  来源:igfitidea点击:

Why does 'Cannot break/continue 1 level' comes in PHP?

phpif-statementfatal-error

提问by Somnath Muluk

I am getting sometimes this error on production at:

我有时会在生产中遇到此错误:

if( true == $objWebsite ) {
    $arrobjProperties = (array) $objWebsite->fetchProperties( );
    if( false == array_key_exists( $Id, $Properties ) ) {
       break;
    }
    $strBaseName = $strPortalSuffix . '/';

    return $strBaseName;
}

$strBaseName = $strSuffix ;
return $strBaseName;

I have tried to reproduce this issue. But not getting any progress. $Id, $Properties having value received.

我试图重现这个问题。但没有任何进展。$Id, $Properties 已收到值。

Does anyone know when does 'Cannot break/continue 1 level' comes in PHP?

有谁知道 PHP 中的“无法中断/继续 1 级”何时出现?

I have seen this post PHP Fatal error: Cannot break/continue. But didn't got any help.

我看过这篇文章PHP Fatal error: Cannot break/continue。但没有得到任何帮助。

回答by Robbie

You can't "break" from an if statement. You can only break from a loop.

你不能从 if 语句中“中断”。您只能中断循环。

If you want to use it to break from a loop in a calling function, you need to handle this by return value - or throw an exception.

如果你想用它来打破调用函数中的循环,你需要通过返回值来处理它——或者抛出一个异常。



Return value method:

返回值方法:

while (MyLoop) {
   $strSecureBaseName = mySubFunction();
   if ($strSecureBaseName === false) {   // Note the triple equals sign.
        break;
   }
   // Use $strSecureBaseName;
}

// Function mySubFunction() returns the name, or false if not found.


Using exceptions - beautiful example here: http://php.net/manual/en/language.exceptions.php

使用异常 - 美丽的例子在这里:http: //php.net/manual/en/language.exceptions.php

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
        else return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>

回答by SudarP

If within a function just change break; to return;

如果在一个函数内只是改变 break; 返回;

回答by Somnath Muluk

If you want to still break from if, you can use while(true)

如果你还想打破if,你可以使用 while(true)

Ex.

前任。

$count = 0;
if($a==$b){
    while(true){
        if($b==$c){
            $count = $count + 3;
            break;  // By this break you will be going out of while loop and execute remaining code of $count++.
        }
        $count = $count + 5;  //
        break;  
    }
    $count++;
}

Also you can use switch and default.

您也可以使用 switch 和 default。

$count = 0;
if($a==$b){
    switch(true){
      default:  
         if($b==$c){
            $count = $count + 3;
            break;  // By this break you will be going out of switch and execute remaining code of $count++.  
        }
        $count = $count + 5;  //
    }
    $count++;
}