Home » Wordpress » how to get errors and line number from php file without running?

how to get errors and line number from php file without running?

Posted by: admin November 30, 2017 Leave a comment

Questions:

i am trying to read line by line from a index.php file and want to display the error and line number of error.This is what i have tried

$code_to_check = "$name = 'soubhagya';echo $name";
$result = eval($code_to_check);

the above coding is not working because i put $ symbol in a variable

Answers:

This is going to solve your problem

$file = 'file.php';
$searchforError = 'content';

header('Content-Type: text/plain');

$contents = file_get_contents($file);
$pattern = preg_quote($searchforError, '/');
$pattern = "/^.*$pattern.*$/m";
if(preg_match_all($pattern, $contents, $matches)){
    echo "Found error:\n";
    echo implode("\n", $matches[0]);
    $charpos = strpos($contents, implode("\n", $matches[0]));
    list($before) = str_split($contents, $charpos);
    $line_number = strlen($before) - strlen(str_replace("\n", "", $before)) + 1;
    echo 'on line '.$line_number;
}
else{
   echo "No errors found";
}