For your first code, you can use a slight change to the answer given by @ShankarDamodaran using in_array in_array() :
if ( !in_array($some_variable, array('uk','in'), true ) ) {
or even shorter with the available notation [] starting with php 5.4 as pointed out by @Forty in the comments
if ( !in_array($some_variable, ['uk','in'], true ) ) {
such as:
if ( $some_variable !== 'uk' && $some_variable !== 'in' ) {
... but shorter. Especially if you are comparing more than just "UK" and "B". I do not use an additional variable (Shankar used $ os), but instead I define an array in the if statement. Some may find it dirty, I find it quick and neat: D
The problem with your second code is that it can easily be replaced simply with TRUE, because:
if (true) {
equals
if ( $some_variable !== 'uk' || $some_variable !== 'in' ) {
You ask if the string value is not A or Not B. If it is A, it is definitely not also B, and if it is B, then it is definitely not A. And if it is C or literally something else, it is also not A and not B. So this statement always (without SchrΓΆdinger's law here) returns the truth.
Andresch Serj Nov 13 '13 at 9:59 2013-11-13 09:59
source share