tags:

views:

47

answers:

4

I'm trying to make errors hidden but it seems I'm doing something wrong. In my hosting configuration display_errors is set to off and I don't have .htaccess file. I tried to write follownng script

<?php
    echo ord(ini_get("display_errors")) . " ";
    die("error");
?>

And I'm getting followng output:

0 error

So, display_errors is set to off, but die() function still shows error on the screen. How to avoid this?

+4  A: 

by doing die("error"), youe are commanding the code that it sohuld output the string "error" and stop the code. You are seeing the "error" message does not mean that there is an error, it is just another string.

die() is a function (commonly used to handle errors), but it is not deactivated when you set display errors off. It will still work and to whatever it is meant to do.

marvin
yeah, commonly used to handle errors by PHP rabble
Col. Shrapnel
@Col. Shrapnel yep
marvin
I thought it supposed to output text to stderr instead of stdout
Poma
@Poma, the [exit() docs](http://us3.php.net/manual/en/function.exit.php) make no mention of stderr or stdout. (die() is just an alias for exit().)
Adam Backstrom
+1  A: 

How to avoid this?

Do not use die() to handle errors.

use trigger_error() instead, which will follow the behavior you expected

Col. Shrapnel
It dont stop the script execution
Poma
Triggering an `E_USER_ERROR` stops script execution. It also puts a log message in Apache's error log (in a normal world).
Adam Backstrom
@Poma THAT'S WHY you should use it! Because terminating script execution is stupid. At least you have to send 500 error and some user friendly HTML first
Col. Shrapnel
@Col. Shrapnel I don't need to do it since I'm writing script for my key management server and only my program will make requests to this script.
Poma
A: 

There is a option to display error to stderr instead of stdout so you won't see them on you webpage, but only in the logs. (Manual)

Hippo
A: 

I found error_log() function. Now I can use

function quit($error)
{
    echo "Request failed";
    error_log($error);
    die();
}
Poma
trigger_error() doing exactly the same but according to error handling settings, silly.
Col. Shrapnel