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
If you are interested in writing for Hyvor Developer, please let us know via email: [email protected]
Latest on Hyvor Developer
Image for Creating a Real-Time Chat App with PHP and Node.js
Creating a Real-Time Chat App with PHP and Node.js
Image for All About MYSQLI Prepared Statements in PHP
All About MYSQLI Prepared Statements in PHP
Image for Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
Image Upload with AJAX, PHP, and MYSQL - The Beginner's Guide
Image for PHP Contact Form - The Email Method
PHP Contact Form - The Email Method
Related Articles
2