PHP Error Control Operator


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 
"; ?>

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

Updated on: 19-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements