0

PHP调试小技巧

 2 years ago
source link: http://pein0119.github.io/2015/06/20/PHP%E8%B0%83%E8%AF%95%E5%B0%8F%E6%8A%80%E5%B7%A7/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP调试小技巧

在开发机环境只能只能使用VIM和Emacs(我用的是Emacs),调试PHP没有什么比较的工具,下面是我常用的一个函数:

file_put_contents('/tmp/my.log', print_r(array(date('Y-m-d H:i:s'), __LINE__, __METHOD__, ), TRUE), FILE_APPEND | LOCK_EX);

该函数可以向/tmp/my.log文件输出一条日志,包括函数执行的日期、在哪一行执行、由哪个方法执行, 在__METHOD__之后可以加上我们想打印的变量,比如$a,然后就像下面这样:

file_put_contents('/tmp/my.log', print_r(array(date('Y-m-d H:i:s'), __LINE__, __METHOD__, $a), TRUE), FILE_APPEND | LOCK_EX);

然后我们重新运行代码,同时新开一个窗口,监视日志变化:

$ tailf /tmp/my.log

这样我们就可以打印出$a变量了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK