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

$hello="world";
echo @$hella; //at
echo isset($hella) ?? null; //issetnull7 >php 7.0 
echo isset($hella) ? $hella : null; //issetnull5

Result:

at issetnull7 issetnull5
0.0017931461334228516 0.00012803077697753906 0.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.

About Jorge Castro

Currently: Entrepreneur and Private Consultant
Civil Engineer in Informatics - USACH Chile.
Master in Business Administration (MBA) CEPADE Spain
Microsoft Certified Professional
Oracle Certified Associate
ScrumMaster Certified
Former developer
Former Project Manager

Related Posts