Question

In an online tutorial I have seen the following snippet of code:

$this->data = $data ?: \Input::all();

Is this a standard ternary operator? What would happen when $data evaluates to true?

Does the following code do the same thing as the original I posted?

$this->data = $data ? null : \Input::all();
Was it helpful?

Solution

It's a ternary operator, shortcut of

 $this->data = $data? $data : \Input::all();

From http://php.net/manual/en/language.operators.comparison.php

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator.

Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top