Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show PHP source code where error occurs?

I once saw a framework or code from some PHP that had an error handler (class or function) that would show an error message, the line number the error occurred on as well as the actual source code, if there was some sort of error in the code, it would show the line or a few lines from the actual PHP file that caused the error. it was really cool, I understand that you would not want to ever do this on a live production server/site but for debugging, it was very nice and something I have never seen before.

I cannot remember where I saw this or how it was done. If you have any ideas on how to do something similar, I would love to have a nice little class for error handling that would do stuff like that, please share any code, ideas, etc on this if you can, really appreciate any help, thank you!

like image 325
JasonDavis Avatar asked Sep 14 '26 18:09

JasonDavis


1 Answers

What you are talking of is a stack trace, in PHP you can retrieve it with debug_backtrace().
Most framework have their own way to display stack trace, but basicly they all use such functions.

However, what you are really looking for is Xdebug it is a debugger and a profilter for PHP, and will let you run code step by step.

  • Stack trace with Xdebug

Edit:

If you want to handle error to provide your own way to work with, you have to use set_error_handler() and set_exception_handler()

A simple example:

<?php
    function myErrorHandler($errno, $errstr, $errfile, $errline) {
      echo "[$errno] $errstr" . PHP_EOL;
      echo "On line $errline in file $errfile" . PHP_EOL;

      $range = array(
          $errline - 5,
          $errline + 5,
       );

       $source = explode(PHP_EOL, file_get_contents($errfile));
       for ($i = $range[0]; $i <= $range[1]; ++$i) {
           if ($i === count($source)) break;
           if ($i === $errline-1) {
               printf("%d | %s  <<<<< Here is the error\n", $i, $source[$i]);
           } else {
               printf("%d | %s \n", $i, $source[$i]);
           }
       }
    }

    set_error_handler('myErrorHandler');

    error_reporting(E_ALL);

    $a = 'Setting variable $a';

    $obj = new Stdclass();
    $obj->foo = 'bar';

    // Oops I'm calling an undefined variable
    echo $undefinedVariable;

    $numbers = array(1,2,3,4,5);
    for($i = 0; $i < 5; ++$i) {
        $numbers[$i] = $i - 1;
    }  

Trying this on cli, should show you:

bguery@joyless:sandbox $ php debugbacktrace.php 
[8] Undefined variable: undefinedVariable
On line 33 in file debugbacktrace.php
28 | $obj = new Stdclass(); 
29 | $obj->foo = 'bar'; 
30 |  
31 | // Oops I'm calling an undefined variable 
32 | echo $undefinedVariable;  <<<<< Here is the error
33 |  
34 | $numbers = array(1,2,3,4,5); 
35 | for($i = 0; $i < 5; ++$i) { 
36 |     $numbers[$i] = $i - 1; 
37 | }   
38 |  

Note that's a really basic usage, but it does the trick. You may need to do more work if you intend to handle E_ERROR, E_PARSE, etc. and you may need to use register_shutdown_function()

like image 187
Boris Guéry Avatar answered Sep 16 '26 07:09

Boris Guéry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!