What is the use of the @ symbol in PHP?

PHPServer Side ProgrammingProgramming

PHP supports the error control operator i.e. the at sign (@). When @ is prepended to an expression, any error messages that might be generated by that expression gets ignored.

To use @ symbol in PHP, the code is as follows−

Example

 Live Demo

<?php
   $file = @file ('non_existent_file') or
   die ("Failed in opening the file: Error Message = '$err'");
   $value = @$cache[$key];
?>

Output

This will produce the following output−

Failed in opening the file: Error Message = ''PHP Notice: Undefined
variable: err in /home/cg/root/6985034/main.php on line 4

Example

Let us now see another example−

 Live Demo

<?php
   $val = $test['5']
   $val = @$test['5']
?>

Output

This will produce the following output−

PHP Parse error: syntax error, unexpected '$val' (T_VARIABLE) in /home/cg/root/6985034/main.php on line 5
raja
Published on 26-Dec-2019 14:52:43
Advertisements