loading...
Cover image for PHP Benchmark @ At symbol

PHP Benchmark @ At symbol

jorgecc profile image Jorge Castro Originally published at southprojects.com ・1 min read

@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:

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.

Posted on by:

jorgecc profile

Jorge Castro

@jorgecc

You are free to believe in whatever you want to, me too. So, stop preaching your religion, politics, or belief. Do you have facts? Then I will listen. Do you have a personal belief? Sorry but no.

Discussion

markdown guide