PHP CLI 模式下,怎么随时调试打印出各个变量值?

2018-06-01 09:10:08 +08:00
 alwayshere

写个爬虫练练手,php cli 模式下进行的,想实时看看当前爬到那个页面了,爬到第几页了,想到用 file_put_contents 来输出当前的变量值,但是感觉太麻烦,能不能直接查看当前 php 的变量值?

3687 次点击
所在节点    程序员
11 条回复
zjsxwc
2018-06-01 09:15:42 +08:00
做成交互界面呗,比如用 ncurses http://php.net/manual/en/intro.ncurses.php
OMGZui
2018-06-01 09:25:02 +08:00
只是想看爬到哪个页面和第几页,比如 for 循环,打印出 url 和 i 不就行了。
zhengwenk
2018-06-01 09:36:21 +08:00
echo $demo."\n"
wangysong
2018-06-01 09:51:21 +08:00
error_log(print_r($data,ture));

做 php 难道不是用这种方法打印调试日志?

如果不是你需要加载一个三方的 log 库了。。

在不影响程序运行的前提下 监控输出结果。。

如果还不满意 想 debug。


xdebug 了解一下?
MeteorCat
2018-06-01 10:04:16 +08:00
<pre>

function watch_info(&$info,$ext_txt = "",$filename = null){

$content ="Time:".date("H:i:s");
$content .= "|type:".gettype($info);
$content .="|data:`";
if(is_array($info)){
$content .= json_encode($info);
}else if(is_null($info))
$content .= "null";
else{
$content .= $info."";
};
$content .="`";
$content .= PHP_EOL;

return file_put_contents(
$filename ? $filename.date("Y_m_d").".log" : "debug_".date("Y_m_d").".log",
$content,
FILE_APPEND
);
}

$info = "test";
$info = array("test");
watch_info($info);

</pre>
这种打印到文件之后,直接`tailf debug_xx_xx_xx.log`打印就 OK 了
`tailf`配合文件监控堪称利器
ioth
2018-06-01 10:05:36 +08:00
换 py
gouchaoer2
2018-06-01 11:05:24 +08:00
用 xdebug+phpstorm 可以做到单步调试的,我就是这么做的
dobelee
2018-06-01 11:14:29 +08:00
封裝一個打印方法就 ok。
RorschachZZZ
2018-06-01 11:27:26 +08:00
直接 echo,记住要换行
echo $msg, PHP_EOL;
update
2018-06-01 11:58:25 +08:00
用 5 楼的方法或者封装个 echo 都可以实现
GoPHP
2018-06-01 12:04:15 +08:00
用 xdebug+phpstorm 可以做到单步调试的,我就是这么做的

+1

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/459450

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX