Cool PHP error-suppressing operator – the @ operator

Today by accident I get new php syntax – the @ operator
I often see code
if ( isset($REQUEST[‘someName’] && … ) {

}

The `…` in the if statement is the real/futher expression usage on
isset($REQUEST[‘someName’]

 

With @, this isset(…) stuff is no longer needed

e.g.
<?php

$a

= array(‘a’=>1, ‘bb’=>22);

echo ‘<h2>error</h2>’;
$b = $a[‘ccc’]==333 ? 1 : 2;
echo $b;

echo ‘<h1>no-error</h1>’;
$b = @$a[‘ccc’]==333 ? 1 : 2;
echo $b;

To me, the @ operator saves & eases myself from worrying if a field in the `$_REQUEST`/array already exists or not. With @, I just call it out and write the expression/condition for it without bothering to start with isset(…) any longer!

Hope you would share this cool operator with me!
Nam

ref.
http://www.php.net/manual/en/language.operators.errorcontrol.php

Advertisements

2 responses to this post.

  1. Posted by N.B. on February 25, 2012 at 5:14 PM

    Quote: “With @, this isset(…) stuff is no longer needed”

    Wrong. The “@” is there to “silence” PHP. The actual error will still go to the error log. You are proposing a terribly bad practice of abandoning isset() function for using silencing operator. That’s so terribly bad that I can’t believe you think it’s a good thing. Just don’t do it, and if you do – don’t blog about it. It’s very, very, very bad thing to do if you are a PHP programmer and use @ instead of isset().

    Reply

  2. Please read to the end my friend. Thank you for you straight comment. I’m not a fool PHP coder though.

    Quote “To me, the @ operator saves & eases myself from worrying if a field in the `$_REQUEST`/array already exists or not. With @, I just call it out and write the expression/condition for it without bothering to start with isset(…) any longer!”

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: