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

2

5

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 ?

Hut answered 28/8, 2013 at 7:59 Comment(0)
B
14

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__
    )
);
Birkett answered 28/8, 2013 at 8:5 Comment(0)
U
1

I prefer this:

error_log(__FILE__ . ':' . __LINE__ . ' something bad happened');
Ume answered 3/6, 2023 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.