Skip to Content
Course content

121: Error Suppression and Why to Avoid It

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've probably come across the @ symbol in some older PHP codebases. It’s called the error suppression operator, and at first glance, it looks like a magic wand. You put it in front of an expression, and suddenly, all those annoying warnings and notices just... vanish. But as someone who has spent way too many hours debugging "silent" failures in production, let me tell you: that magic wand is actually a trap.

Trying to load a config file quietly

Let's say we're building a small feature that loads a user's custom theme settings from a JSON file. I want the app to check if a theme.json file exists and load it. If it doesn't, I just want it to fall back to the default settings without screaming at the user with a PHP warning.

My first instinct—and this is the mistake I'm showing you—is to just suppress the error if the file is missing. I'll write it like this:

<?php
$settingsFile = 'user_theme.json';
// I don't want a warning if the file is missing, so I'll just suppress it
$data = @file_get_contents($settingsFile);

if ($data === false) {
    $settings = ['color' => 'blue', 'font' => 'Arial']; // Defaults
} else {
    $settings = json_decode($data, true);
}
?>

On the surface, this "works." If the file is gone, PHP doesn't trigger a Warning: file_get_contents(...), and the user gets the default blue theme. It feels clean. But here's where things go south.

The moment I realized I'm flying blind

A few days later, I notice that some users are reporting the default theme is loading even though they definitely uploaded a config file. I go to check the logs, and... nothing. Absolute silence.

Because I used @, I didn't just suppress the "file not found" warning; I suppressed everything. It turns out the files were there, but the permissions were wrong, or the disk was full, or the file was locked. PHP tried to tell me exactly what was wrong, but I told it to shut up. I spent an hour guessing why the code was failing because I had intentionally blinded my own error reporting.

Using @ is like putting a piece of tape over the "Check Engine" light in your car. The light is gone, but the engine is still smoking.

Handling the absence of data explicitly

The professional way to handle this isn't to hide the error, but to prevent it from happening in the first place. I need to ask PHP if the file is actually available before I try to touch it. This makes the code's intent clear to anyone else reading it and keeps the error logs useful.

Here is how I refactored that logic:

<?php
$settingsFile = 'user_theme.json';

// Instead of suppressing, I'll check the state of the filesystem first
if (file_exists($settingsFile) && is_readable($settingsFile)) {
    $data = file_get_contents($settingsFile);
    $settings = json_decode($data, true);
} else {
    // Now I know exactly why we're using defaults: the file isn't there or isn't readable
    $settings = ['color' => 'blue', 'font' => 'Arial'];
}
?>

Now, if file_get_contents fails for some weird, catastrophic reason, I'll actually see that error in my logs. More importantly, I've replaced a "hack" with actual logic. If you find yourself wanting to use @, stop and ask yourself: "What specific condition am I afraid will trigger an error?" Once you identify that, use a function like file_exists(), isset(), or is_dir() to handle it explicitly.




📋 Practical Task

Refactoring a Suppressed Database Path Checker

You've inherited a piece of legacy code that checks for the existence of a database backup directory. The previous developer used error suppression to ignore warnings when the directory doesn't exist.

Your Task: Rewrite the following code snippet to remove the @ operator. Replace it with a proper conditional check that ensures the directory exists and is actually a directory before attempting to read its contents.

<?php
$backupDir = '/var/backups/db_dumps';
// BAD: Suppressing the warning if the directory is missing or inaccessible
$files = @scandir($backupDir);

if ($files === false) {
    echo "No backups found.";
} else {
    echo "Backups found: " . count($files);
}
?>
Rating
0 0

There are no comments for now.

to be the first to leave a comment.