PHP -抓取抛出错误的代码


PHP - Grab the code that threw an error

我希望能够抓取实际的代码行(不是数字,是代码文本行),其中发生了NOTICE错误。有什么办法可以做到这一点吗?在当前运行的脚本中,我似乎找不到一个函数可以返回某一行#上的代码。

假设PHP脚本具有读取当前文件的权限,您可以这样做:

<?php
echo $b; // Undefined variable
$errors = error_get_last();
$errorMessage = $errors['message'];    
$pathToScript = $errors['file'];
$line = $errors['line'];
$arrayOfLines = file(__FILE__);
echo "The error message was: '$errorMessage occured in $pathToScript'";
echo "The line of code that caused the error is: 'n";
highlight_string($arrayOfLines[$line-1]);
输出:

错误信息是:'未定义变量:b发生在/path/to/script.php'

导致错误的代码行是:echo $b;