PHP-else和#x27;逃逸';嵌套并跳到elseif

PHP-else和#x27;逃逸';嵌套并跳到elseif,php,if-statement,nested,continue,Php,If Statement,Nested,Continue,我不太确定如何命名这个问题-以下是我正在做的一个片段: <?php if ($result_rows >= 1 && $membership = 'active') { if ($when_next_allowed > $today_date) { $output = 'You cannot renew your membership for another <b>' . $days_left . 'days</

我不太确定如何命名这个问题-以下是我正在做的一个片段:

<?php
  if ($result_rows >= 1 && $membership = 'active') {
     if ($when_next_allowed > $today_date) {
         $output = 'You cannot renew your membership for another <b>' . $days_left . 'days</b>.';
     }
     /*
     What if the membership is set to active, but it's been over a year since they
     activated it? We don't have any server-side functions for determining such
     at the time.
     */
     else {
         /* do database stuff to change the database entry to inactive */
         /* skip to elseif below */
     }
  }
  elseif (2 == 2) {
     /* create new database entry for user's membership */
  }
?>
但我从来没有听说过,也不知道有什么方法可以像那样使用“原始”形式的变量。当然,我已经对它做了研究,尽管我还没有找到它的原理。我不确定这是常识还是什么——但我从来没有听说过或考虑过这样的事情,直到现在


解决方案?这是我一直在思考的问题,尽管我从来都不知道我必须使用它。

如果我正确理解了这个问题,那么你可以这样做:

if (1==1 && 1==2) {
    /* ... */
}
elseif (2==2) {
    $success = 'Success';
}
显然,我不需要指出,
1==1&&1==2
是完全不合逻辑的,只是用作两个布尔语句的示例


根据问题的更新进行更新:

除非您省略了其他步骤,否则这将复制您的逻辑。很难知道这是否真的解决了您的问题,因为我不知道
2==2
代表什么,也不知道根据其他条件您可能需要执行哪些其他步骤

if (($result_rows >= 1 && $membership == 'active') && 
    ($when_next_allowed > $today_date)) {
        $output = 'You cannot renew your membership for another <b>' . $days_left . 'days</b>.';
}
elseif (2 == 2) {
 /* create new database entry for user's membership */
}
if($result\u rows>=1&&$membership=='active')&&
($when\u next\u allowed>$today\u date)){
$output='您不能再续订您的会员资格。$days\u剩余。'days';
}
elseif(2==2){
/*为用户的成员身份创建新的数据库条目*/
}

这应该是您想要做的。 如果您将一个变量设置为false,并在进入else时将其切换为true,那么您只需在进入elseif后立即测试该变量的值即可

<?php

    $test = false;

    if (1 == 1) {

        if (1 == 2) {

            /* ... */

        }

        else {

            /* Skip to elseif below */
            $test = true;
        }

    }

    if ($test == true) {

        $success = 'Success';

    }

    echo $success;
?>

这不是一个简单的问题,因为很难理解您想要实现的目标,但我认为这是您正在寻找的解决方案

<?php

$success = False;

if (1 == 1) {
    if (1 == 2) {
        /* ... */
    } else {
        $success = True;

        /* True case code can go here */
    }
}

echo $success;

?>

伪代码是你的朋友

或者

<?php

$success = False;

if (1 == 1) {
    if (1 == 2) {
        /* ... */
    } else {
        $success = True;
    }
}

if $success == True {
    /* ... */
}

echo $success;

?>

我能想到的最干净的:

$run = false;
if (1 == 1) {
    $run = true;
    if (1 == 2) {
        /* Do something */
    } else {
        $run = false;
        /* Do something else */
    }
}

if (!$run && 2 == 2) {

}
或者,您可以在[Do something other]和第二个if块之间使用goto,但无论哪种方式都会很混乱

if (1 == 1) {
    if (1 == 2) {
        /* Do something */
    } else {
        /* Do something else */
        goto 1
    }
} else if (!$run && 2 == 2) {
    1:
}

坦率地说,看起来你已经陷入了
if
/
else
意大利面。如果你退一步思考一下这个过程,或者至少提供一个更具体的例子,可能有一个简单的方法可以解决大部分问题。@cHao-我会用一个更真实的表达我正在做的事情来修饰这个问题。更新。我有理由不想删除过期的成员资格数据或更新其过期日期。在什么情况下,您不会创建新的数据库条目?@不要惊慌-如果用户的成员资格仍然设置为活动且未过期。我可以花几个小时来解释为什么我有两个条目来决定“成员资格”,但简而言之,Database表作为成员拥有的虚拟“项目”的“项目”表加倍——“成员资格”本身就是一个“项目”。如果Super Cat希望在1==1而不管1==1的情况下运行代码,这不一定正确2@tommoyang没错,但是看看Super Cat的代码,在第一个
if(1==1){…}
块中除了检查下一个
if(1==2)
之外,没有执行任何操作。所以你可能是对的,但是没有更多的细节我不知道。我的逻辑只是复制并简化了他的第一个例子。
$run = false;
if (1 == 1) {
    $run = true;
    if (1 == 2) {
        /* Do something */
    } else {
        $run = false;
        /* Do something else */
    }
}

if (!$run && 2 == 2) {

}
if (1 == 1) {
    if (1 == 2) {
        /* Do something */
    } else {
        /* Do something else */
        goto 1
    }
} else if (!$run && 2 == 2) {
    1:
}