在PHP中跨程序运行时打印/记录变量的状态?

时间:2012-12-01 19:28:33

标签: php debugging xdebug

所以我有一段 long 的PHP代码,它使用loops / if语句修改全局数组。

是否有任何直接方法可以在对其进行的每次更改中打印数组的值?也许使用XDebug?

我更喜欢使用文本日志而不是使用XDebug进行逐行调试。

3 个答案:

答案 0 :(得分:0)

嗯,通过输出JS来实现它的一种方法: 安装Firebug,然后您可以使用console.log(...)console.debug(...)等(see the documentation)。

答案 1 :(得分:0)

Xdebug支持函数跟踪,它将每个函数调用转储到磁盘上的文件中(参见http://xdebug.org/docs/execution_trace)。从最近的版本开始,函数跟踪也可以配置为显示变量的变化。您可以使用xdebug.collect_assignment设置启用它(请参阅:http://xdebug.org/docs/execution_trace#collect_assignments)。

答案 2 :(得分:0)

如果没有xdebug,您可能必须定义一个自定义对象(实现ArrayAccess)而不是数组,并登录offsetSet,快速模拟启动:

   class ArrayLogger implements ArrayAccess {
       protected $array;
       public function __construct(array $array){
           $this->array = $array;
       }
       public function offsetGet($idx){
           return $this->array[$idx];
       }
       public function offsetExists($offset) {
           return isset($this->array[$offset]);
       }
       public function offsetUnset($offset) {
           unset($this->array[$offset]);
       }
       public function offsetSet($offset,$value){
           //do some logging here
           debug_print_backtrace();
           var_dump($this->array,$offset,$value)
           //and actually, you may need to inspect $value for arrays. & convert them all to ArrayLogger's before setting this:
           $this->array[$offset] = $value;
       }
   }
相关问题