How to get the line of code that executes a function from the function in PHP

1 Answer

0 votes
function get_code_line_caller($var) {
    $trace = debug_backtrace();
    $this_file_source_code = file( __FILE__ );
     
    $the_line_code_that_call_to_this_function = $this_file_source_code[$trace[0]['line'] - 1];
 
    return $the_line_code_that_call_to_this_function;
}
   
$num = 23452;
echo get_code_line_caller($num);
   
$str = "PHP";
echo get_code_line_caller($str);
   
$arr = array(1, 2, 3);
echo get_code_line_caller($arr);
   
     
    
/*
run:
   
echo get_code_line_caller($num);
echo get_code_line_caller($str);
echo get_code_line_caller($arr);
    
*/

 



answered Jun 12, 2024 by avibootz
edited Jun 12, 2024 by avibootz
...