1

I have a series of if/elseif conditional statements using the same structure and syntax, and I'd like to clean up and condense the code within a foreach to more easily add and remove items as the list grows. What I'm starting with looks something like this:

if ( $condition1 == 1 && ( $condition2 == 1 || $condition3 == 1 ) ) {
  $var = 'string 1';
} elseif ( $condition1 == 2 && ( $condition2 == 2 || $condition3 == 2 ) ) {
  $var = 'string 2';
} elseif ( $condition1 == 3 && ( $condition2 == 3 || $condition3 == 3 ) ) {
  $var = 'string 3';
} elseif ( $condition1 == 4 && ( $condition2 == 4 || $condition3 == 4 ) ) {
  $var = 'string 4';
} else {
  $var = 'default string';
}

Ideally, I would structure this more like:

$conditions = array(
  'string 1'    => 1,
  'string 2'    => 2,
  'string 3'    => 3,
  'string 4'    => 4,
);
foreach ( $conditions as $key => $value ) {
  if ( $condition1 == $value && ( $condition2 == $value || $condition3 == $value ) )
    $var = $key;
}
if ( empty($var) )
  $var = 'default string';

The biggest issue here is that I want all if statements after the first iteration to be elseif to in case multiple conditions are met so I can control the hierarchy by the order of the items in the array. Also, this approach may be totally wrong and there could be a much better way to approach the problem altogether, so I'm open to any and all critique!

1 Answer 1

2

You can rearrange the code a bit to do what you need. break out of the loop if a condition is met ("simulating" the elseif conditions), and place the default before the loop, so, if no condition is met, the value will be that one:

$conditions = array(
  'string 1'    => 1,
  'string 2'    => 2,
  'string 3'    => 3,
  'string 4'    => 4,
);
$var = 'default string';
foreach ( $conditions as $key => $value ) {
  if ( $condition1 == $value && ( $condition2 == $value || $condition3 == $value ) ) {
    $var = $key;
    break;
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Looks like I misunderstood. You don’t need only one condition met? If two are met, so you want to have two values for $var?
Sorry about that, I deleted my comment but you read too fast. :) Your solution appears to work perfectly for what I need. Appreciate it!
Are you sure? This will indeed stop execution after meeting a condition (I though that's what you wanted). But re-reading your question, you seem to need more than one condition meet.
I only need one condition to be met to define the variable once, so this is good for me.
Great! I was a bit confused, since it seems like you can't meet multiple conditions? Because of the left hand side of the && operation (only can have one value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.