PHP Line Number constant

PHP has a constant for the current line number, and the current filename. Useful for debugging as you can pepper them through your code and see where it stops working.

print __FILE__ . " " . __LINE__;
Leave A Reply - 5 Replies
Replies
Blaster_Boy 2008-08-28 09:30am - No Email - Logged IP: 98.172.25.66

Thanks for this tip. Here is a way to automatically pepper this throughout your code:

cat program.php | sed 's/;$/; print FILE . " O.K. to line: " . LINE . "\n";/' > program-debug.php

Enjoy!

Steven 2008-09-10 01:45pm - No Email - Logged IP: 78.143.208.155

Fantastic. Now I have an include in all my pages, which can be called with: debug(); It's great!

function debug(){ echo "File:".FILE . "
Line:" .LINE."
"; }

JJ 2010-04-22 04:46am - jj@tradeinternet.co.uk - Logged IP: 62.31.61.56

the function won't work as far as I can see because it will always give you the line number of the function, not the calling code. Am I missing something?

JJ 2010-04-22 05:05am - No Email - Logged IP: 62.31.61.56

Take a look at http://us2.php.net/manual/en/function.debug-backtrace.php this function provides more information and provides data on calling line & file

Chris Hester 2010-05-09 03:28pm - ch@christopherhester.co.uk - Logged IP: 87.114.178.8

Thanks for this! I have the function set up to pass the line number to it by using a variable. I do not need the filename outputting myself.

function debug($lineno) { echo 'PHP line no: '.$lineno.''; }

debug(LINE);

All content licensed under the Creative Commons License