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

后端 未结 3 1188
南旧
南旧 2021-02-18 19:55

I am getting sometimes this error on production at:

if( true == $objWebsite ) {
    $arrobjProperties = (array) $objWebsite->fetchProperties( );
    if( false         


        
3条回答
  •  太阳男子
    2021-02-18 20:10

    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.


    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

    getMessage(), "\n";
    }
    
    // Continue execution
    echo 'Hello World';
    ?>
    

提交回复
热议问题