PHP, Cómo poner un error en un lugar específico

You know how PHP errors always show up at the very top of the page. Is there a way to suppress that and turn it in to a $var? Then you could place it somewhere in the page.

preguntado el 10 de marzo de 12 a las 02:03

Ideally, you would write your code so that it produces no errors or warnings. This is not only possible, but highly recommended as good practice. -

2 Respuestas

Si, podrías configura tu propio controlador de errores to intercept and do something with those error besides simply outputting them wherever they occur. But I don't think it's very useful to display PHP errors elegantly on a page. PHP errors are not meant to be pretty, they're not meant to occur at all. If you see one, it should be as jarring as possible and you should fix it as soon as possible. They're meant as a help during development. Trying to pretty them up is spending time on the wrong part of the code.

In production, those errors should be logged to a log file and not visibly output on the page at all.

respondido 10 mar '12, 02:03

This is kind of one-use. $php_errormsg lets you handle each error individually instead of having to reset the handler any time you want it to handle errors differently. - Jon Egeland

Pero $php_errormsg requiere que anticiparse a where an error could occur and to clean out the variable after you have handled the error. That's mucho más "one-use" then a custom global error handler. And it seems to me the OP is asking for a global error handler. - deceze

$php_errormsg holds the previous error, even the suppressed ones.

@fopen('foo.txt', 'w');

echo $php_errormsg; // You can put this anywhere you want the error to display

If you think you'll have more than one error, then you could store these errors in an array.

$errors = array();

for($files as $f) {
  @fopen($f, 'w');
  $errors[] = $php_errormsg;
}

Then you could print out all your errors:

print_r($errors);
  //or
for($errors as $e) {
  echo $e;
}

respondido 10 mar '12, 02:03

log_errors = on y display_errors = off in php.ini is much, much cleaner. - ladrónmaestro

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.