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

2019-03-22 09:18发布

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?

2条回答
做自己的国王
2楼-- · 2019-03-22 09:34

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]
查看更多
老娘就宠你
3楼-- · 2019-03-22 09:38

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.

查看更多
登录 后发表回答