如何使用Javascript显示浏览器中的所有PHP运行时错误 [英] How to show all PHP runtime errors in browser using Javascript

查看:232
本文介绍了如何使用Javascript显示浏览器中的所有PHP运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在开发周期中,当编程PHP和测试时,每次运行都会看到一个错误。



您可以使用php.exe或使用一个在线工具 pilliapp phpcodechecker 。但是,运行时错误,如未定义的变量,您无法检测。



您可以使用 XDebug 进行跟踪,但无聊。



如何在浏览器中显示所有PHP运行时错误?



用户可以同时处理所有愚蠢的问题,以不引人注意的方式。 (不是逻辑错误)。

解决方案

论坛和Q& A网站确实是一个很好的学习资源!



昨天我做了一个问题 stackoverflow .com和讨论激励我创建一种新的方式来驯服PHP中绝大多数运行时错误使用 PHP Javascript



假设我有一个具有许多潜在运行时错误的PHP源代码。所以我把我的代码放在HTML标签中的以下行中,例如在初始位置,我需要使用PHP代码。

 < script type =text / javascript> 
var nerrors = 0; // PHP错误计数在Javascript

函数PHPErrors(){//包装PHP代码的JavaScript函数
<?php
// MyErrorHandler是我的自定义函数错误处理程序
//以给定顺序使用错误参数。

函数myErrorHandler($ errno,$ errstr,$ errfile,$ errline){
echodocument.getElementById('PHPError')。innerHTML + =。
\$ errno:$ errstr($ errline)< br> \; \r\\\
;
echonerrors = nerrors + 1; \r\\\
;
返回true;
}
//这是我的定制新的错误处理
//禁用默认错误处理
$ old_error_handler = set_error_handler(myErrorHandler);

//下面的随机PHP代码充满错误
$ b = $ a; // PHP错误1
$ c = $ a +1; // PHP错误2
$ d = $ a + 1; // PHP错误3
$ e = $ _SESSION ['glove']; // PHP错误4
?>
//没有PHP运行时错误值得一个很好的messagen
如果(nerrors === 0){
document.getElementById('PHPError')。innerHTML =
完成! PHP没有错误。 }
} //结束JavaScript函数

此代码后可以添加额外的Javascript和PHP自如。
最后我把我的一些地方放在HTML body里面的2行。

 < p id =PHPError > < / p为H. 
< button onclick =PHPErrors()>显示所有PHP运行时错误< / button>

所以当我浏览这个PHP页面,我可以看到





最后我按下它,我可以看到所有PHP运行时错误消息

  8:未定义的变量:a(26)
8:未定义的变量:d(27)
8:未定义的变量:c(28)
8:未定义的索引:手套(29)
/ pre>

或愉快的

 做得好! PHP没有错误。 

显然,不需要任何按钮,可以将这个代码包含在javascript onload函数中。


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).

解决方案

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.

这篇关于如何使用Javascript显示浏览器中的所有PHP运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆