Do I need to specify not to use the second variable in my second else if statement? If so, how?

StackOverflow https://stackoverflow.com/questions/20393601

سؤال

If I want to create an if statement with 2 variables:

if ($variable1 && $variable2) {
    // Do something
}

And then add another if statement below with only the first variable, how would I do it? Do I only include the one variable like this:

else if ($variable1) {
     // Do something
}

Or do I need to specify that the first variable is true, not the second? If so, is this correct?

if ($variable1 && !$variable2) {
    // Do something
}
هل كانت مفيدة؟

المحلول

Go for:

if ($variable1 && $variable2) {
    // Do something
}

else if ($variable1) {
     // Do something
}

the reason is for example if you write like this :

the following is wrong approach

if ($variable1) {
        // if u have two variables $variable1 and $variable2
        // and you want to validate both but if the $variable1 contains
       //  nonzero value it will never go to the else part
    }

    else if ($variable1 && $variable2) {
         // Do something
    }

now basically

else if ($variable1) {
     // Do something
}

and

 else  if ($variable1 && !$variable2) {
        // Do something
    }

are same.you can use any of them if you are not toooo much concerned about the performance.

نصائح أخرى

else if ($variable1) {
     // Do something
}

is enough. Since the first if-statement will fail, it will evaluate the else if as a new statement

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top