What to use instead of if!

x contains tree kind of p,s
it is array, I just simplifys it a bit.
What should I use?
All three is true and I want all tree to be executed.
If dosent work, it stops after the first if.

foreach(x as p){
when( x = p1){ do something }
when( x = p2){ do something }
when( x ! = p1 && x ! = p2){ do something }
}

Your question isn’t very clear. It sounds like you know what you’re doing, you just want to see some other options for doing it? That’s called refactoring. If that’s what you’re after, add your current working code to the question then we can give some suggestions.

I know what I want to do, but not how!

when(x = 1){ do that}
when(x = 2){ do this}
when(x not are 1 eller 2){ do the other}

If I use if instead of when, it stops after first if.
I don’t know what to use.

OK, can you show us the non working code?

If you want to do one thing you do not use an “IF”. If you want to do two or more things with conditions, you just nest your “IF” statements. It is the basics of IF’s…
if (some-condition) {
do something
} elseif (some-condition) {
do something else
} elseif (some-third-condition) {
do something else else…
}
Now, this process of nested IF’s means if the first or second CONDITION is true, it never gets to the third.
Sometimes you need to put in extra IF’s inside the “do-something” areas depending on what you need.
But, I think you want to just use simple ELSEIF’s…

By the way, elseif can be spelled else if … I do not use the space, but, it works…

Ok, thanks
But what if all three conditions are true,
and I want all three to execute?
like:
Maybe I need a loop?

for( x = p1){ do something }
for( x = p2){ do something }
for( x ! = p1 && x ! = p2){ do something }

Well, you said you want to do something based on three conditions and run one process.
So, I gave you an example of that.

If you want to handle all three possibilities just handle separate IF’s without the else. Perhaps you are wanting it this way. If x=p1 do something else if x=p2 do something else do a third thing. This is done something like this:
if (x==p1) {
. . . do process #1
} elseif (x==p2) {
. . . do process #2
} else {
. . . do process #3 if #1 and #2 fail . . .
}
As you see logic is important when you think these processes out. In this example, it handles p1 first, if it succeeds, then the entire process is finished, but, if it is not p1, then it moves on to p2 and if that fails does the final fall-back process… Is that what you need?

Oh, also, in your first post, you had a foreach to loop thru a data array. You wrote it wrong. It would create a variable named “p” but you use x in your tests. So, you really would need to handle it more like this:
foreach($x as $data) {
if($data==p1) {
. . . etc…

I think what you are looking for is a
switch:

switch(x){
case p1:
do something;
case p2:
do something;
//break;
default:
do something else;
}

This will check each case but only start executing code when one of the cases is true. Everything after that will be executed even if the next case is false; if you need to prevent further execution use a break. If no cases are true then the default will execute.

Thanks so much. That was just what I wanted. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service