1

Instead of writing out

if ($var1 = $something && $var2 == $something)
    echo 'this';

is it possible to write something similar to this instead

if ($var1 && $var2 == $something)
    echo 'this';
  • 1
    Why do you want to write this expression in another way? – Salman A Mar 27 '13 at 07:44
  • no, it is not possible – Sumit Bijvani Mar 27 '13 at 07:45
  • no. why would you want to? if typing the extra few characters is a problem write a class or function. in my opinion that would just make your code harder to read though. also you need to use the double == not the singel. = is an assignment operator for assigning variables and == is for comparing values. – I wrestled a bear once. Mar 27 '13 at 07:44

2 Answers2

0

no, not really. There is no shortcutting operator in PHP that does what you describe, though you could write a function in PHP that saves on typing if you're doing this comparison regularly.

 function allEqual($value1, $value2, $value3) {
     return $value1 == $value2 && $value2 == $value3;
 }

 // Usage:
 if( allEqual( $var1, $something, $var2 ) ) {
Dai
  • 141,631
  • 28
  • 261
  • 374
0
$a = array($var1, $var2, $something);

if(count(array_unique($a)) == 1){
  // all match
}
else {
  // some items do not match
}

Ref: PHP: The best way to check if 3 variables are identical?

Community
  • 1
  • 1
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73