Search code examples
phpissetsuperglobals

Using isset or @ to test/assign a superglobal variable


Can I use @ instead of isset to assign or test superglobal variable ?

Use this :

$foo = intval(@$_POST['bar']);

Instead of this :

$foo = isset($_POST['bar']) ? intval($_POST['bar']) : 0;

works without generate a notice but maybe for some reasons, the use of isset is better than @ ?


Solution

  • isset with the ternary operator would be cleaner and easier to read. Error suppression on the other hand, has some overhead costs:

    I first built a simple test that would loop a million times accessing a variable with and without the suppression operator prepended. The differences were small, yet noticeable. Using the suppression operator ended up taking 40% longer to execute.

    Sources:

    http://seanmonstar.com/post/909029460/php-error-suppression-performance http://www.lunawebs.com/blog/2010/06/07/another-look-at-php-error-supression-performance/