介绍几个高级PHP调试函数(debug_zval_dump和debug_backtrace)

1.debug_zval_dump(),它与var_dump()的区别就是它新增了一个值refcount,即记录变量被引用的次数。同时它还可以打印几个变量。

如果你连引用计数器都不懂的话,请阅读:PHP垃圾回收机制引用计数器概念

 

  1. <?php
  2. $a = "www.phpddt.com";
  3. $b = &$a;
  4. //如果你安装了xdebug的话可以用xdebug_debug_zval()
  5. xdebug_debug_zval( 'a' ); //refcount=2, is_ref=0
  6.  
  7. //debug_zval_dump() 函数只输出了 php 内部实现的引用计数机制,
  8. //如果程序中使用 & 取地址符 不被算在内,同样它也没有输出is_ref 的值
  9. debug_zval_dump($a); //refcount(3)

 

结果如下:

a:

(refcount=2, is_ref=1),

string 'www.phpddt.com' (length=14)

string(14) "www.phpddt.com" refcount(1)

 2.debug_print_backtrace() debug_backtrace() 只是前者直接打印出来了而已。查看整个程序的调用栈,用来查看瞬间函数调用栈,方便查错。

看看手册上这个例子:

 

  1. <?php
  2. function a() {
  3. b();
  4. }
  5. function b() {
  6. c();
  7. }
  8. function c(){
  9. debug_print_backtrace();
  10. }
  11. a();
  12. ?>

 

结果如下:

#0 c() called at [E:\www\test\debug.php:6]

#1 b() called at [E:\www\test\debug.php:3]

#2 a() called at [E:\www\test\debug.php:11]

这里只是简单测试,后期会贴出实战教程,这些跟踪和调试函数是绝对的利器。

posted @ 2016-12-26 21:40  天涯海角路  阅读(824)  评论(0)    收藏  举报