What is the use of the @ symbol in PHP?

error-suppression, operators, php

Solution

The `@` symbol is the error control operator (aka the "silence" or "shut-up" operator). It makes PHP suppress any diagnostic error messages generated by the expression associated with it. Like unary operators, it has a precedence and associativity. Below are some examples:

@echo 1 / 0;
// displays "Parse error: syntax error, unexpected T_ECHO"
// this is because "echo" is not an expression
// the script terminates because of parse error

echo @(1 / 0);
// suppressed "Warning: Division by zero"

@$i / 0;
// suppressed "Notice: Undefined variable: i"
// displays "Warning: Division by zero"

@($i / 0);
// suppressed "Notice: Undefined variable: i"
// suppressed "Warning: Division by zero"

$c = @$_POST["a"] + @$_POST["b"];
// suppressed "Notice: Undefined index: a"
// suppressed "Notice: Undefined index: b"

$c = @foobar();
echo "Script was not terminated";
// displays "Fatal error: Call to undefined function foobar()"
// fatal errors are displayed even with the shut-up operator
// the script terminates because of fatal error

Problem

I have seen uses of `@` in front of certain functions, like the following: ``` $fileHandle = @fopen($fileName, $writeAttributes); ``` What is the use of this symbol?

Original source

Related problems