3

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Super Cat
  • 1,547
  • 1
  • 18
  • 23
  • Frankly, it looks like you're knee-deep in `if`/`else` spaghetti. If you step back a bit and think about the process, or at least provide a more concrete example, there's probably an easy way to get rid of most of it. – cHao Aug 07 '14 at 21:36
  • @cHao - I'll spruce up the question a bit with a more solid representation of what I'm doing. – Super Cat Aug 07 '14 at 21:38
  • Updated. I have my reasons for not wanting to delete the expired membership data or update it's expiration date. – Super Cat Aug 07 '14 at 21:50
  • In what case would you not create a new database entry? – Don't Panic Aug 07 '14 at 21:53
  • @Don't Panic - In the case that the user's membership is still set to active and not expired. I could spend hours explaining why I have _two_ entries determine the 'membership', but in a nutshell, the the datatbase table doubles as an "item" table for virtual "items" the member owns - The 'membership' is an 'item' itself. – Super Cat Aug 07 '14 at 21:56

5 Answers5

4

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:
}
tommoyang
  • 329
  • 1
  • 6
2

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 */
}
Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • This isnt necessarily correct if Super Cat wants to run code if 1 == 1 regardless of 1 == 2 – tommoyang Aug 07 '14 at 21:38
  • @tommoyang That's true, but looking at Super Cat's code, there is nothing executed in the first `if(1==1) {...}` block, other than the checking of the next `if(1==2)`. So you might be right but without more details I don't know. My logic simply replicates and simplifies his first example. – Mark Miller Aug 07 '14 at 21:41
1
<?php
  $continue = false;
  if (1 == 1) {

     if (1 == 2) {

     /* ... */

     }

     else {

     $continue = true;

     }

  }

  if ($continue==true) {

     $success = 'Success';

  }

 echo $success;

?>
tommoyang
  • 329
  • 1
  • 6
Renan
  • 21
  • 2
1

This should do what you want to do. If you have a variable to false and switch it to true if you go into the else you want, you just have to test the value of this variable right after to go into elseif you wanted to go in.

<?php

    $test = false;

    if (1 == 1) {

        if (1 == 2) {

            /* ... */

        }

        else {

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

    }

    if ($test == true) {

        $success = 'Success';

    }

    echo $success;
?>
Alex
  • 478
  • 2
  • 11
1

Not an easy question as it's really hard to understand what you're trying to achieve but I think this is the solution you're looking for.

<?php

$success = False;

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

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

echo $success;

?>

pseudo code is your friend.

Alternatively;

<?php

$success = False;

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

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

echo $success;

?>
South Paw
  • 1,230
  • 1
  • 9
  • 11