Rodolpho Freire
Rodolpho Freire

Reputation: 210

Errors hidden with @ in php

I am repairing some old code that was made before me and I took about 2 hours to find a error because it was hidden by @.

Is that a way to deactivate this in PHP?

display_errors don't get errors hidden by @

Upvotes: 1

Views: 128

Answers (2)

Michael Irigoyen
Michael Irigoyen

Reputation: 22947

You can enable track_errors to save whatever error was generated by the error control operator (@) to a PHP variable. You can set track_errors in your php.ini file or use ini_set.

If the track_errors feature is enabled, any error message generated by the expression will be saved in the variable $php_errormsg. This variable will be overwritten on each error, so check early if you want to use it.

Example

<?php
ini_set('track_errors', true);
@strpos();
echo $php_errormsg;

Will output:

strpos() expects at least 2 parameters, 0 given

Caveats

It is important to note that critical errors that cause the script to terminate that are suppressed by @ will not be discoverable this way. In this case, if your script dies unexpectedly and without an error message, that should be a good indication to search for the @ in your code.

Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.

Upvotes: 2

fejese
fejese

Reputation: 4628

You can define a custom error handler as described in the php documentation for error control operator

Upvotes: 3

Related Questions