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

I wasn't sure what to call this question - Here is a snippet of what I am 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 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 membership */
  }
?>

      

If the first nested argument is false , it should jump to else , which should continue from there and "exit" from the parent, if and jump to elseif . Another wise one , if the first nested argument is true , then it should stay put .

Is it even possible? The only thing I could think of would be to add some commands continue;

. This is, of course, a mistake.

Another idea of ​​mine was to set the variable equal to else and then set it right up to the end of the parent if: continue;

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

      

But I have never heard and do not know any method of using variables in such a "raw" form. Of course, I've been doing research, although I have yet to figure out how to do this. I'm not sure if this is general knowledge or something else. But I never heard of it or thought it until now.

Decision? This is what I have always thought about, although I never knew I would have to use it.

+3


source to share


5 answers


The 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 can use a goto between the [Do Something Else] and the 2nd if block, but that would be messy anyway.

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

      

+4


source


If I understand the problem correctly, you can simply do something like this:

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

      

Obviously I don't need to point out which 1==1 && 1==2

is completely illogical and is just being used as an example of two boolean operators.




Update based on update to question:

If there are no additional steps that you omit, this repeats your logic. It's hard to know if this really solves your problem because I don't know what it represents 2==2

, or what other steps you might need to follow based on 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 membership */
}

      

+2


source


<?php
  $continue = false;
  if (1 == 1) {

     if (1 == 2) {

     /* ... */

     }

     else {

     $continue = true;

     }

  }

  if ($continue==true) {

     $success = 'Success';

  }

 echo $success;

?>

      

+1


source


This should do what you want to do. If you have a variable set to false and switch it to true, if you change to a different location, you just need to check the value of that variable right after you jump to the elseif you would like to enter.

<?php

    $test = false;

    if (1 == 1) {

        if (1 == 2) {

            /* ... */

        }

        else {

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

    }

    if ($test == true) {

        $success = 'Success';

    }

    echo $success;
?>

      

+1


source


Not an easy question as it is difficult to understand what you are trying to achieve, but I think this is the solution you are looking for.

<?php

$success = False;

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

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

echo $success;

?>

      

pseudocode is your friend.

As an alternative;

<?php

$success = False;

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

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

echo $success;

?>

      

+1


source







All Articles