Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - else, 'escape' nesting and skip to elseif

I wasn't too sure how to title this question - Here's a snippet of what I'm doing:

<?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 the first nested argument is false, it should move onto else which should continue from there and 'escape' the 'parent' if and move onto elseif. Other wise, if the first nested argument is true, then it should stay put.

Is that even a possible occurrence? The only thing I could think of was to add multiple continue; commands. That, of course, threw an error.

One other idea I had was setting a variable to equal continue; within the else, then set that right before the end of the parent if:

if (1 == 1) {
...
  else {
       $escape = 'continue;';
  }
/* $escape here */
}

But I've never heard of, nor do I know of any method of using variables in a 'raw' form like that. Of course I've done research on it, though I've yet to find out how. I'm not sure if that's common knowledge or anything - But I've never heard of, or considered such a thing until now.

Solution? This is something I always thought about, though I never knew I'd have to use it.

like image 702
Super Cat Avatar asked Feb 03 '26 05:02

Super Cat


2 Answers

Cleanest I could come up with:

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

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

}

Alternatively, you could use a goto between [Do something else] and the 2nd if block, but it'll be messy either way.

if (1 == 1) {
    if (1 == 2) {
        /* Do something */
    } else {
        /* Do something else */
        goto 1
    }
} else if (!$run && 2 == 2) {
    1:
}
like image 177
tommoyang Avatar answered Feb 05 '26 18:02

tommoyang


If I understand the problem correctly, then you could just do something like this:

if (1==1 && 1==2) {
    /* ... */
}
elseif (2==2) {
    $success = 'Success';
}

Obviously, I don't need to point out that 1==1 && 1==2 is completely illogical and is just used as an example of two boolean statements.


Update based on update to question:

Unless there are additional steps that you are omitting, this replicates your logic. Hard to know if this really solves your problem, because I don't know what 2==2 represents, or what other steps you might need to perform based on what other conditions.

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 */
}
like image 35
Mark Miller Avatar answered Feb 05 '26 19:02

Mark Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!