How to return a line number in a PHP script

You can use the magic constant __LINE__ for this.

echo  __LINE__;

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


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__)


You could use something like this:

function throwSQLError($file, $line, $error) {
    return "MySQL-Error in " . $file . " on line " . $line . ": " . $error;
}
$result = mysql_query("Some SQL;") or die (throwSQLError(__FILE__, __LINE__, mysql_error()));

This gives you a dynamic function to throw the error you can use multiple times.

Untested, just written in raw editor.

Tags:

Php

Debugging