PHP Error Control Operator

PHPServer Side ProgrammingProgramming

Introduction

In PHP @ symbol is defined as Error Control Operator. When it is prefixed to any expression, any error encountered by PHP parser while executing it will be suppressed and the expression will be ignored.

Following code tries to open a non-existing file for read operation, but PHP parser reports warning

Example

 Live Demo

<?php
$fp=fopen("nosuchfile.txt","r");
echo "Hello World \n";
?>

Output

Following result will be displayed

Hello World
PHP Warning: fopen(nosuchfile.txt): failed to open stream: No such file or directory in /home/cg/root/1569997/main.php on line 2

Prepending @ symbol to fopen() expression suppresses error message and statement itself is ignored

Example

 Live Demo

<?php
$fp=@fopen("nosuchfile.txt","r");
echo "Hello World";
?>

Output

Following result will be displayed

Hello World
raja
Published on 19-Sep-2020 13:42:18
Advertisements