What is @ in PHP?

When reading PHP source codes, you may have found the symbol @ used sometimes. So, what's @ in PHP?

What is @?

@ is an error controlling operator. If you prepend it to a PHP expression, the expression won't be throwing any errors. All the errors will be ignored. (Remember it as the "shut-up" operator)


<?php
$name = $_POST['name'];
$name = @$_POST['name'];

In this example, the first expressiom will throw an error if there's no post param "name" is set. But, the second one won't be throwing any error.

Here's another example


<?php
$i = 9;
echo @$i / 0; // Error thrown
echo @($i / 0); // No error

Simply, @ is affects the expression right after it. (By using parentheses, you can group several expressions into one)

Besides all of these, it is not a good practise to use @ as it shuts up PHP.

However, I found some interesting uses of the @ operator while reading some source codes.


// PHPMailer() library uses this 
@mail($to, $subject, $body, $header);

// from CODOForum
// TIP: Apache sets the Server variable HTTPS to "on" when requested via HTTPs
define('PROTOCOL', @$_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://');

If you know more uses of the @ operator please let me know via the comments below.

Thank you for reading.

Tagged: PHP
You can connect with me on Twitter or Linkedin.
Latest on My Blog
PHP Beginner's Tutorial
Beginner's PHP Tutorial
Image for Laravel High CPU Usage Because of File-based Session Storage
Laravel High CPU Usage Because of File-based Session Storage
Image for Resizing Droplets: A Personal Experience
Resizing Droplets: A Personal Experience
Image for Moving our CDN (10+ GB images) to a new server
Moving our CDN (10+ GB images) to a new server
Image for Disqus, the dark commenting system
Disqus, the dark commenting system
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Related Articles
23