A quick question that someone might be able to help me with. I am interested in finding out the name of the file and line number that is calling a function. For example I have this file


<?php
function testLine() {

echo "File: " . __FILE__ . "<br />\nLine: " . __LINE__;

}
testLine();// I want this line number
?>


When I call testLine it echos out the file and line #4 which is the expected behaviour. However, I want to find a way to find out the line number where it was called, which in this case would be 7. I know I can pass FILE & LINE to the function, I just would rather not have to pass that data every time. If anyone knows of a way I can do this I would be very grateful.

Thanks,
Bryan

    assuming the file you're looking in is called "file.php":

    <?
    $file = file ('file.php');
    foreach ($file as $key => $value)
    {
    if (stristr ($value, 'testLine()'))
    {
    $line = $key++;
    break;
    }
    }
    echo 'testLine() appears on line # ' . $line;
    ?>

    The above example will quit after it finds the first occurance of "testLine()". If you want to find every occurance, I would suggest you modify the above code to compile the various lines into an array.

      Write a Reply...