[If Statement] Comparing value to multiple integers
#1

Hi all,

I want to compare a variable against a lot of different integers. I tried the following, expecting it to fail and it did. However, it should give you an idea of what I'm trying to do:

Код:
if(myVariable != 1 && 2 && 8 && 12 && 13 && 14 && 15){
How do I actually do this, with out having 'myVariable' in the if statement a million times over?

Thanks
Reply
#2

You can't.
Reply
#3

PHP код:
switch(myVariable)
    case 
12812131415: {}
    default: {
        
//bla bla
    
}

Reply
#4

Or if you really want to use if-else, and also want to compare strings or multiple variables then don't move to switch.

It should be something like this -

Код:
if(YourVariable != 1 && YourVariable != 1) // goes on.
Reply
#5

Quote:
Originally Posted by AbyssMorgan
Посмотреть сообщение
PHP код:
switch(myVariable)
    case 
12812131415: {}
    default: {
        
//bla bla
    
}

Thanks, good idea.

Quote:
Originally Posted by [ND]xXZeusXx.
Посмотреть сообщение
Or if you really want to use if-else, and also want to compare strings or multiple variables then don't move to switch.

It should be something like this -

Код:
if(YourVariable != 1 && YourVariable != 1) // goes on.
Thanks, but this is the point of my question, and what I want to avoid.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)