PHP为什么“无法打破/继续1级”? [英] Why does 'Cannot break/continue 1 level' comes in PHP?

查看:94
本文介绍了PHP为什么“无法打破/继续1级”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

我试图重现此问题。但没有取得任何进展。 $ Id,$已收到价值的房产。

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

有没有人知道什么时候不能打破/继续1级来自PHP?

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

我看过这篇文章 PHP致命错误:无法中断/继续。但没有得到任何帮助。

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

推荐答案

你不能从if语句中破解。你只能从一个循环中断。

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

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

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.

返回值方法:

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.






使用例外 - 这里有一个很好的例子: 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';
?>

这篇关于PHP为什么“无法打破/继续1级”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆