tree em
tree em

Reputation: 21711

get the line and page at the calling in PHP?

I have 02 pages

1.php

  function def($var){
      echo "$var";
    debug();
      }

2.php (calling page)

 def("test");
  debug();

create a debug small function

    function debug(){
     echo "called in script".$script;
     echo "<br>"
     echo "called at line".$at_line;
      }

How can I implement the function debug() to get the information as described?

EDIT: I know we can use

$file = $_SERVER["SCRIPT_NAME"]//to get the script name

What is difference

  $arr = debug_backtrace();

  $file = $arr[0]['file'];//to get the script name

Which ways should use for my case.?

Upvotes: 0

Views: 202

Answers (3)

deceze
deceze

Reputation: 522042

You could easily do it inline using the magic constants __FILE__ and __LINE__.
If you want to use a debug function call, you have to backtrack to the previous call using debug_backtrace.

Upvotes: 2

John Chadwick
John Chadwick

Reputation: 3213

You want debug_backtrace. To see what kinds of information you can get out of it, try running this:

var_dump(debug_backtrace());

in your debug function.

Upvotes: 1

Headshota
Headshota

Reputation: 21449

you can use the magic constants:

PHP Magic Constants

Upvotes: 0

Related Questions