Search code examples
phpternary

Ternary statement without middle expression


How to replace this :

if( $this->getInfo() ){
   $value = $this->getInfo();
}else{
   $value = $this->getAnotherInfo();
}

This would be nicer solution :

$value = $this->getInfo() ? $this->getInfo() : $this->getAnotherInfo();

But we repeat $this->getInfo().


Solution

  • Here is the fun:

    $value = $this->getInfo() ? : $this->getAnotherInfo();