Search code examples
phperror-loggingerror-log

How to save the line number when using error_log in PHP?


When using error_log(..) in PHP I would like to specify the line where the error occurred :

error_log("something bad happened on line $LINE");

How can I do that ?


Solution

  • You should use a Magic constant called __LINE__, so:

    error_log("something bad happened on line ".__LINE__);
    

    Another useful magic constant in this context may be __FILE__ for the filename:

    error_log(
        sprintf(
            "%s:%d: something bad happened",
            __FILE__,
            __LINE__
        )
    );