Home » Php » debugging – How to return a line number in a PHP script

debugging – How to return a line number in a PHP script

Posted by: admin April 23, 2020 Leave a comment

Questions:

Is it possible for a PHP script to return a line number in which some command is called? I’m having trouble describing what I want so maybe an example.

I have PHP code that calls MySQL on many occasions. In line 49 is:

$resultDevice = mysql_query("Some SQL;") or die ("MySQL-Error in settingsU line 49: " . mysql_error());

The text “line 49” I wrote manually. Is it possible to get this number “49” updated if I change my code? It would make my life easier to debug. Of course I can put some other line-specific text into die, but lines are much easier to find in a text-editor.

How to&Answers:

You can use the magic constant __LINE__ for this.

echo  __LINE__;

will show the line number where that statement is in the file.

Answer:

if you want a trace you can use(For PHP 4 >= 4.3.0:)

function error_report_function($error) 
{
$dbgt=debug_backtrace();
return "$error in {$dbgt[1][file]} on line {$dbgt[1][line]}";
}

You can use debug_backtrace for this or else always pass the line (with __LINE__)