在php中退出嵌套的if语句?

时间:2012-01-08 05:06:18

标签: php if-statement conditional

我有这段代码:

if (condition){  //number1
    if (condition){  //number2
        action
    }
}
else{
    action
}

现在,如果嵌套的if语句,数字2,结果为false,是否可以将程序“重定向”到外部的else语句,而不是像这样创建一个嵌套的else语句?

if (condition){  //number1
    if (condition){  //number2
        action
    }
    else {
        action;
    }
}
else {
    action
}

编辑:这是我的实际代码:

if ($s == 5){  //which award is won
    $id_raw = $wpdb->get_var("SELECT user_ID FROM ".$wpdb->prefix."award".$award." WHERE user_ID='".$p_uid."'"); //check to see if user has already won this award
    if($id_raw==''){ // if he/she didnt make entry saying they did
        $wpdb->query("INSERT INTO ".$wpdb->prefix."award1 (user_ID, isTold) VALUES(".$p_uid.", 'false') ") or die(mysql_error());
        wp_delete_post( $pid, true );
    }
}
else{ 
    cp_points('voteitup_user_up', $uid, get_option('cp_module_voteitup_user_up'), $pid);
    cp_points('voteitup_author_up', $p_uid, get_option('cp_module_voteitup_author_up'), serialize(array($pid,$uid)));
}   

2 个答案:

答案 0 :(得分:3)

只是猜测你在这里真正想做什么,因为你的id_raw = ''是一个无意义的条件而且问题不明确:

$insert = $id == 5 && !$wpdb->get_var("SELECT user_ID FROM ".$wpdb->prefix."award".$award." WHERE user_ID='".$p_uid."'");
if ($insert) {
    $wpdb->query("INSERT INTO ".$wpdb->prefix."award1 (user_ID, isTold) VALUES(".$p_uid.", 'false') ") or die(mysql_error());
    wp_delete_post( $pid, true );
} else {
    cp_points('voteitup_user_up', $uid, get_option('cp_module_voteitup_user_up'), $pid);
    cp_points('voteitup_author_up', $p_uid, get_option('cp_module_voteitup_author_up'), serialize(array($pid,$uid)));
}

希望这会给你一个想法。

答案 1 :(得分:0)

为什么不结合前两个if语句?你需要在那里做其他事吗?

if (condition1 && condition2) {
    action
}
else {
    action
}