Is it possible to retrieve the number that the error line of code is on?

Closest I have come to is mysql_error() but that does not tell me the correct number the line of code is on just like line 1 and it isnt?

Line of Code: In your text editor everyline is coded from 1,2,3,etc

Thanks, Regards X

Dani AI

Generated

— there are more reliable ways to get the file and line of an error than scanning source text. ’s file-scanning trick can work, but it’s brittle (depends on unique strings, breaks with includes/encodings and won’t catch fatal/parse errors). A more robust approach is to convert runtime errors into exceptions, catch them, and use the exception API to get file/line and a stack trace.

Use a custom error handler that throws ErrorException, then catch it and read getFile() / getLine() (see set_error_handler and ErrorException):

set_error_handler(function($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
});

try {
    // risky code
} catch (ErrorException $e) {
    echo 'Error in ' . $e->getFile() . ' on line ' . $e->getLine() . ': ' . $e->getMessage();
    echo PHP_EOL . $e->getTraceAsString();
}

For fatal errors (which stop execution), use a shutdown handler with error_get_last() to log the last error and its file/line: see register_shutdown_function and error_get_last.

register_shutdown_function(function() {
    $err = error_get_last();
    if ($err && in_array($err['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR])) {
        error_log("Fatal: {$err['message']} in {$err['file']} on line {$err['line']}");
    }
});

For development, enable a proper debugger (Xdebug) to get readable stack traces and IDE integration (Xdebug docs). Also review debug_backtrace when you need programmatic frame inspection. Finally, follow dev vs production rules: show errors locally, but log them and keep display_errors off in production (error handling config).

Recommended Answers

All 7 Replies

If you use die function and if an error occurs and prints the die message, then its pretty obvious where exactly the error has occurred !
For runtime errors, php parser will only let you know where the error has occurred if you have error_reporting turned on.

Ya I was using the die function but it was causing me problems but if you have multiple die statements then :p, which you would have seen in the previous threads.

My error reporting is as follows, after reading the comments it seems this is the best anyways "error_reporting = E_ALL & ~E_NOTICE"

Ya I was using the die function but it was causing me problems but if you have multiple die statements then :p, which you would have seen in the previous threads.

;) Then, you will fix the errors one by one !

My error reporting is as follows, after reading the comments it seems this is the best anyways "error_reporting = E_ALL & ~E_NOTICE"

Yep! In my opinion, that is the best option of error reporting. :)

If you are talking about the line number the die() function is used on then the following is an alternative die function that will show the line number.

function dieline($stringz) {
$dataz=file_get_contents(basename($_SERVER['PHP_SELF']));
$datazz=explode($stringz,$dataz);
$rowz=substr_count($datazz[0], '
'); //must start at beginning of line
die ('<b>Error Line number '.$rowz.'</b><br>'.$stringz);
}
//now to use it
$result=mysql_query('SELEC * FROM `table`') or dieline('Error 001'.mysql_error());

The only thing to keep in mind is that the string you enter into the die function needs to be unique from all the other strings in the file. So the best way to avoid the wrong line number from comming up is having the phrase 'Error 001' before the mysql error reporting except replace the number with a unique number from all the other die() / dieline() functions.

If you are talking about the line number the die() function is used on then the following is an alternative die function that will show the line number.

function dieline($stringz) {
$dataz=file_get_contents(basename($_SERVER['PHP_SELF']));
$datazz=explode($stringz,$dataz);
$rowz=substr_count($datazz[0], '
'); //must start at beginning of line
die ('<b>Error Line number '.$rowz.'</b><br>'.$stringz);
}
//now to use it
$result=mysql_query('SELEC * FROM `table`') or dieline('Error 001'.mysql_error());

The only thing to keep in mind is that the string you enter into the die function needs to be unique from all the other strings in the file. So the best way to avoid the wrong line number from comming up is having the phrase 'Error 001' before the mysql error reporting except replace the number with a unique number from all the other die() / dieline() functions.

Hi Cwarn...
Can u explain me what is basename...

file_get_contents([B]basename[/B]($_SERVER['PHP_SELF']));

basename is the filename within the path inputed. So say the filename was located /folder1/folder2/folder3/index.php basename would convert it to index.php

basename is the filename within the path inputed. So say the filename was located /folder1/folder2/folder3/index.php basename would convert it to index.php

Hi Thank u Cwarn....
oooh..its nice...i was struggling many times.... how can i get only filename....now i got it...

Thank u..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.