开发者

How do I incorporate the line number of php source code in a custom error message?

开发者 https://www.devze.com 2023-02-14 17:48 出处:网络
I want to do my proper error generator when i\'m programming (HTML + PHP). How can i take 开发者_如何转开发the line, when i have an error, and put in a variable?

I want to do my proper error generator when i'm programming (HTML + PHP). How can i take 开发者_如何转开发the line, when i have an error, and put in a variable?

Example :

echo " Error # 03: variable undefined line #".$line." ";

Thanks.


the variables you'd be looking for are:

__LINE__
__FILE__
__FUNCTION__
__CLASS__


there is a predefined constant, __LINE__ that contains the line where it was actually called.

However, I guess that trigger_error() function perfectly fits "error generator" term, thus being exactly what you're looking for.
It will not only show you a line and a file and a timestamp, but also will follow general behavior of PHP error reporting settings, which is very important - you should never echo errors implicitly but rather put it into standard error stream

for the custom error handler there is also a debug_backtrace() function.


Assuming you are referring to PHP compile time errors and warnings, the line number is automatically displayed. Since these messages are generated at compile time (and as such can cause the script not to execute fully) I would recommend using the default messages rather than using a custom solution.

If PHP is not displaying the error messages, use the following code to display all of the PHP error messages and warnings on a page:

error_reporting(E_ALL);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号