How to change the way php errors are displayed?

Asked

Viewed 918 times

1

Since I formatted the server I can no longer change the way errors were displayed. Today errors are presented as follows Exibição de erro atual

and I would like it to be shown as in WAMP

inserir a descrição da imagem aqui

My php.ini file is configured as follows:

display_errors = On
error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE
html_errors = On

Version of php

PHP Version 5.5.13-1~dotdeb.1

Server

Debian 7 "Wheezy"
  • 1

    See if the Xdebug is installed

  • @lost not installed yet, I will test with it if change anything

  • I tested in windows, I removed only the comment from the line [XDebug]
zend_extension = caminho php.ini and the error message came with that orange.

  • I’m installing Xdebug here, it’s full of dependencies, which have their dependencies, and debian doesn’t make life easy at all kkkkk

  • @lost was the same Xdebug :D puts as answer so I can mark as correct

  • I think you better write the answer and put what steps/commands you ran on linux and also if you needed to change php.ini or ext folder to install Xdebug.

  • Okay, I’ll do that then

  • 1

    If you just want a more beautiful and organized way to visualize errors, it is worth taking a look at this project: https://github.com/JosephLenton/PHP-Error

  • a good thing of the answer / option using set_error_handler is that it frees you from the need to configure Xdebug (option this may not even be available on shared host) at the same time it gives you total freedom to work the layout of the wrong screen ;-)

  • Actually on shared servers do not have this possibility, but on these servers I will not display errors, I have to display them for production and then hide any error that may occur for aesthetic reasons to the client

Show 5 more comments

2 answers

2

You must use set_error_handler and set a function of yours to handle and display errors as you wish

Source: http://www.php.net/manual/en/function.set-error-handler.php

<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }

    //CUSTOMIZAR SUAS TELAS DE ERRO AQUI

    switch ($errno) {
    case E_USER_ERROR:
        echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
        echo "  Fatal error on line $errline in file $errfile";
        echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
        echo "Aborting...<br />\n";
        exit(1);
        break;

    case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
        break;

    case E_USER_NOTICE:
        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
        break;

    default:
        echo "Unknown error type: [$errno] $errstr<br />\n";
        break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}

// function to test the error handling
function scale_by_log($vect, $scale)
{
    if (!is_numeric($scale) || $scale <= 0) {
        trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
    }

    if (!is_array($vect)) {
        trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
        return null;
    }

    $temp = array();
    foreach($vect as $pos => $value) {
        if (!is_numeric($value)) {
            trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
            $value = 0;
        }
        $temp[$pos] = log($scale) * $value;
    }

    return $temp;
}

// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");

// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
print_r($a);

// now generate second array
echo "----\nvector b - a notice (b = log(PI) * a)\n";
/* Value at position $pos is not a number, using 0 (zero) */
$b = scale_by_log($a, M_PI);
print_r($b);

// this is trouble, we pass a string instead of an array
echo "----\nvector c - a warning\n";
/* Incorrect input vector, array of values expected */
$c = scale_by_log("not array", 2.3);
var_dump($c); // NULL

// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
$d = scale_by_log($a, -2.5);
var_dump($d); // Never reached
?>

1


To do this you must install Xdebug. As it is full of dependencies we have to install some things first

Step 1: Install the PEAR

apt-get install php-Pear

Step 2: Install PHP5-DEV

apt-get install php5-dev

Step 3: Install Libcurl3

apt-get install libcurl3-openssl-dev

Step 4: Install PECL-HTTP (optional)

pecl install pecl_http

Step 5: Install Xdebug :D

pecl install Xdebug

Step 6: Add the following line in your php.ini

zend_extension=Xdebug.so

And that’s it, now your php will display the errors with the orange box, the cascading arrays, will be able to debug the code among other things...

Sources:
Chirale
MKFOSTER
Xdebug - Official website

Browser other questions tagged

You are not signed in. Login or sign up in order to post.