打破家长IF(有条件)

时间:2012-12-15 19:29:34

标签: php conditional break

如果在PHP中如何破坏父级?

示例此代码:

if(true){ // This is parent conditional
    if(true) { // This is child conditional
        break parent conditinal
    }
}

afterBreakDoMe();

从上面的代码。当孩子有条件的时候我想要。然后它打破父条件(退出该条件)并继续其余代码(afterBreakDoMe())。

更新 - 真实代码

    $errorMessage = '';
    if(isset($_POST) && count($_POST)) { // Detect some user input
        // validation
        $countryName = trim($_POST['countryName']);         
        if($countryName == ''){ // validation user input, if false. exit from parent if and continue show html input (refer to $Render->output())
            $errorMessage = 'Name must not be empty!'; 
        }

        header('Location: '.$baseUrl.'&page=tableShipping');
    }

    $Render->setTitle('Create New Country');
    $form = $Render->parser('createCountry', array(
        'errorMessage' => $errorMessage,
    ));
    $Render->addContent($form);

    $Render->output();

6 个答案:

答案 0 :(得分:4)

你不能这样做(除了使用goto,这是禁忌),不应该这样做。简单交换功能是一个坏主意。

根据您的“真实”代码,您的程序流程没有意义。

if($countryName == ''){ // validation user input, if false. exit from parent if and continue show html input (refer to $Render->output())

所以,如果它是true,你将设置$errorMessage,然后退出,离开页面,再也不用了它?为什么?您也可以忘记错误消息,拨打header(),然后拨打exit;

我根据您想要发生的事情创建了以下代码,并将其评为解释:

$errorMessage = '';
if(isset($_POST) && count($_POST)) { // Detect some user input
    // validation
    $countryName = trim($_POST['countryName']);         
    if($countryName == ''){ // validation user input, if false. exit from parent if and continue show html input (refer to $Render->output())
        $errorMessage = 'Name must not be empty!'; 
    } else {
        // If there is no error, continue forward.
        header('Location: '.$baseUrl.'&page=tableShipping');
        exit; // Leave the page execution since you've applied a header.
    }
}

// This will only execute if there is an error, since we left the page otherwise.
$Render->setTitle('Create New Country');
$form = $Render->parser('createCountry', array(
    'errorMessage' => $errorMessage,
));
$Render->addContent($form);

$Render->output();

总而言之,请考虑您的计划流程,以确定接下来会发生什么,而不是要跳过的内容

答案 1 :(得分:1)

简单

if (true) {
   if (true) {
       // Do stuff
       // It will then break automaticly
       // but if the condition here is false you got the else ;)
   } else {

   }
}

答案 2 :(得分:1)

使用goto。这用于跳转代码

答案 3 :(得分:1)

$errorMessage = '';
do {
     if(isset($_POST) && count($_POST)) { // Detect some user input
        // validation
        $countryName = trim($_POST['countryName']);         
        if($countryName == ''){ // validation user input, if false. exit from parent if and continue show html input (refer to $Render->output())
            $errorMessage = 'Name must not be empty!';
            break;
        }

        header('Location: '.$baseUrl.'&page=tableShipping');
    }
while(false);// only for break

答案 4 :(得分:1)

现在我可以阅读真正的代码示例了,我认为你需要做的就是简单地将所有条件放在一起。使用goto将完成工作,但很容易使您难以阅读和维护代码。也许是时候重新考虑程序流程了。 无论如何,我所说的是这样的:


if(!empty($_POST) && isset($_POST['countryName']) && (trim($_POST['countryName']) == '')){
    $errorMessage = 'Name must not be empty!'; 
}

...

答案 5 :(得分:1)

您可以使用变量和额外的if语句:

$errorMessage = '';
$emptyCountry = false;
if(isset($_POST) && count($_POST)) { // Detect some user input
    // validation
    $emptyCountry = trim($_POST['countryName']) == '';
    if($emptyCountry){ // validation user input, if false.
        $errorMessage = 'Name must not be empty!'; 
    }

    header('Location: '.$baseUrl.'&page=tableShipping');
}
if (!emptyCountry) {
    $Render->setTitle('Create New Country');
    $form = $Render->parser('createCountry', array(
        'errorMessage' => $errorMessage,
    ));
    $Render->addContent($form);
}

$Render->output();