How to show all PHP runtime errors in browser using Javascript

六眼飞鱼酱① 提交于 2020-01-06 01:30:08

问题


Normally in development cycle when one is programming PHP and testing, one sees just one error for each run.

You can see syntax errors using php.exe or using a online tool pilliapp or phpcodechecker. However, runtime errors, like undefined variable, you cannot detect.

You can use XDebug for tracing but it's boring.

How one can show all PHP runtime errors once in browser?

So the user can handle all silly problems at the same time, in unobtrusive way. (Not the logical errors).


回答1:


Forums and Q&A websites are indeed a great resource for learning!

Yesterday I've made a question in stackoverflow.com and the discussion inspire me to create a new way to tame the vast majority of runtime errors in PHP using PHP and Javascript.

Suppose I have a PHP source code with many potential runtime errors. So I put my code in the following lines inside HTML tag, e.g. in the initial position that I need to use PHP code.

 <script type="text/javascript">
 var nerrors = 0;               // PHP Errors count in Javascript

 function PHPErrors() {         // Javascript  Function that wraps the PHP Code
  <?php
               // MyErrorHandler is my function that my custom error handler
               // uses with error parameters in that given order.

     function myErrorHandler($errno, $errstr, $errfile, $errline) {
     echo "document.getElementById('PHPError').innerHTML += " . 
      "\"$errno: $errstr ($errline)<br>\";\r\n";        
     echo "nerrors = nerrors + 1;\r\n";
     return true;
 }
      // That's the line the set my custom new error-handling
      // disabling the default error handling
 $old_error_handler = set_error_handler("myErrorHandler");

      // Below random PHP code full of errors 
 $b = $a;                 // PHP Error 1
 $c = $a +1;              // PHP Error 2
 $d = $a + 1;             // PHP Error 3  
 $e = $_SESSION['glove'];  // PHP Error 4
?>
      // No PHP runtime error deserves a nice messagen
if (nerrors===0) {
   document.getElementById('PHPError').innerHTML =
                    "Well done! PHP without errors."; } 
}                        // End Javascript Function

After this code one can put additional Javascript and PHP freely. Finally I put in my some place inside HTML body the 2 below lines.

    <p id="PHPError"> </p>
    <button onclick="PHPErrors()">Show me all PHP runtime errors</button> 

So when I browse this PHP page, I can see

Finally I press it and I can see all PHP runtime error messages

8: Undefined variable: a (26)
8: Undefined variable: d (27)
8: Undefined variable: c (28)
8: Undefined index: glove (29)

or the pleasant

Well done! PHP without errors.

Obviously one don't need any button and it is possible to include this code inside a javascript onload function.



来源:https://stackoverflow.com/questions/14358518/how-to-show-all-php-runtime-errors-in-browser-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!