Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Suppressing and Restoring Errors | PHP Error Types and Reporting
Practice
Projects
Quizzes & Challenges
Quizzen
Challenges
/
PHP Error Handling

bookSuppressing and Restoring Errors

When writing PHP scripts, you may encounter situations where you want to suppress specific errors temporarily. PHP provides the error control operator, which is the at symbol (@), for this purpose. By placing @ in front of an expression, you can suppress any error messages that expression might generate. This operator is often used when you expect a function to potentially fail and want to handle the error silently or in your own way, rather than displaying a warning or notice to the user.

However, the error control operator only affects the expression it directly precedes. It does not change the global error reporting settings or affect other parts of your script. If you want to temporarily change the error reporting level for a section of your code, you can use the error_reporting() function to adjust which errors PHP will report. After your temporary change, you should restore the previous error reporting settings to avoid unintended side effects elsewhere in your script.

suppress_restore.php

suppress_restore.php

copy
123456789101112131415161718
<?php // Save current error reporting level $old_error_reporting = error_reporting(); // Suppress all errors temporarily error_reporting(0); // This will not show any warning, even if file.txt does not exist $content = @file_get_contents("file.txt"); if ($content === false) { echo "Could not read file.\n"; } // Restore previous error reporting settings error_reporting($old_error_reporting); // This will show a warning if file.txt does not exist $content2 = file_get_contents("file.txt");

While using the error control operator (@) can be convenient, it comes with significant risks. Suppressing errors can make debugging much harder, as you may miss important warnings or notices about problems in your code. Overuse of @ can hide bugs and make your code less reliable and maintainable. Instead of suppressing errors, you should handle them explicitly—such as by checking return values and using proper error handling logic. If you must suppress errors, do so only for very specific cases and always restore error reporting settings after making temporary changes, as shown in the code above. This approach helps you avoid masking issues that could lead to bigger problems later on.

question mark

What is a major risk of using the error control operator (@) in PHP?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 4

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookSuppressing and Restoring Errors

Veeg om het menu te tonen

When writing PHP scripts, you may encounter situations where you want to suppress specific errors temporarily. PHP provides the error control operator, which is the at symbol (@), for this purpose. By placing @ in front of an expression, you can suppress any error messages that expression might generate. This operator is often used when you expect a function to potentially fail and want to handle the error silently or in your own way, rather than displaying a warning or notice to the user.

However, the error control operator only affects the expression it directly precedes. It does not change the global error reporting settings or affect other parts of your script. If you want to temporarily change the error reporting level for a section of your code, you can use the error_reporting() function to adjust which errors PHP will report. After your temporary change, you should restore the previous error reporting settings to avoid unintended side effects elsewhere in your script.

suppress_restore.php

suppress_restore.php

copy
123456789101112131415161718
<?php // Save current error reporting level $old_error_reporting = error_reporting(); // Suppress all errors temporarily error_reporting(0); // This will not show any warning, even if file.txt does not exist $content = @file_get_contents("file.txt"); if ($content === false) { echo "Could not read file.\n"; } // Restore previous error reporting settings error_reporting($old_error_reporting); // This will show a warning if file.txt does not exist $content2 = file_get_contents("file.txt");

While using the error control operator (@) can be convenient, it comes with significant risks. Suppressing errors can make debugging much harder, as you may miss important warnings or notices about problems in your code. Overuse of @ can hide bugs and make your code less reliable and maintainable. Instead of suppressing errors, you should handle them explicitly—such as by checking return values and using proper error handling logic. If you must suppress errors, do so only for very specific cases and always restore error reporting settings after making temporary changes, as shown in the code above. This approach helps you avoid masking issues that could lead to bigger problems later on.

question mark

What is a major risk of using the error control operator (@) in PHP?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 4
some-alt