In php , is there a way to trace a variable?

2019-03-22 09:16发布

问题:

I have this variable in a big php script that I want to trace back to where/when and what value it was instantiated. Is there a function/api or debugging techniques to do this?

回答1:

If you're using PHPStorm you can set breakpoints and inspect the values of variables.

http://blog.jetbrains.com/phpstorm/2013/12/just-in-time-debugging-and-php-exception-breakpoints-with-phpstorm-and-xdebug/

You'll need xdebug installed as well.



回答2:

You can use debug_print_backtrace() function. http://php.net/manual/en/function.debug-print-backtrace.php

<?php


function f1() {
    f2();
}

function f2() {
    f3();
}

function f3(){
    echo "<pre>";
    debug_print_backtrace();
    echo "</pre>";
}

f1();

?>

Output:

#0  f3() called at [/home/xfiddlec/public_html/main/code_47406510.php:9]
#1  f2() called at [/home/xfiddlec/public_html/main/code_47406510.php:5]
#2  f1() called at [/home/xfiddlec/public_html/main/code_47406510.php:18]