If we assume that you're taking 'efficiency' to mean run time (vs other measures such as number of keystrokes, e.g.) then what you originally have is as efficient as it gets:
if($var == $var1 || $var == $var2)
Conditional operators ||
and &&
short circuit when they can, meaning if $var
happens to equal $var1
, then the comparison against $var2
will never take place.
If you start to have many many conditions are you're just looking to keep your code clean, you can store all of the possible values in an array and check to see if $var
is in the array:
$possibleVals = array( 1, 2, 3 );
$var = 1;
if(in_array($var, $possibleVals)) {
// ...
That, however, is not nearly as efficient in terms of running time than if you listed out all of the comparisons individually (for a nominal number of elements you most likely wouldn't even notice a difference, though).