PHP Benchmark At symbol
@at symbol
Depending the configuration of PHP, if we try to access an undefined variable, it could show an error.
Example:
$hello="world";
echo $hella; // <-- we show hella instead of hello
The system will show the next message:
Notice: Undefined variable: hella in php.php on line 3
We could disable it but it is not recommended (at least in our development ambiance). Why? Because typo happens.
One way to solve it, is to use the symbol at (@)
$hello="world";
echo @$hella;
It does the next job, if the variable is defined, then it shows the value. Otherwise, it will show a null.
But is it efficient?
Benchmark time
https://github.com/EFTEC/php-benchmarks#isset-vs--at
$hello="world";
echo @$hella; //at
echo isset($hella) ?? null; //issetnull7 >php 7.0
echo isset($hella) ? $hella : null; //issetnull5
Result:
atissetnull7issetnull50.00179314613342285160.000128030776977539060.0001201629638671875
Conclusion: @ is considerably slower. Also, there is little difference between ?? and the ternary operation.
why?. It is because @ is a special operation, it captures an error and it is a slow operation. Also, isset is a language construction (it’s not a function), so isset is fastest.