V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
alwayshere
V2EX  ›  程序员

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

  •  
  •   alwayshere · 2018-06-01 09:10:08 +08:00 · 3654 次点击
    这是一个创建于 2128 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

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

    如果还不满意 想 debug。


    xdebug 了解一下?
    MeteorCat
        5
    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
        6
    ioth  
       2018-06-01 10:05:36 +08:00
    换 py
    gouchaoer2
        7
    gouchaoer2  
       2018-06-01 11:05:24 +08:00
    用 xdebug+phpstorm 可以做到单步调试的,我就是这么做的
    dobelee
        8
    dobelee  
       2018-06-01 11:14:29 +08:00 via Android
    封裝一個打印方法就 ok。
    RorschachZZZ
        9
    RorschachZZZ  
       2018-06-01 11:27:26 +08:00
    直接 echo,记住要换行
    echo $msg, PHP_EOL;
    update
        10
    update  
       2018-06-01 11:58:25 +08:00
    用 5 楼的方法或者封装个 echo 都可以实现
    GoPHP
        11
    GoPHP  
       2018-06-01 12:04:15 +08:00
    用 xdebug+phpstorm 可以做到单步调试的,我就是这么做的

    +1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5319 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 05:46 · PVG 13:46 · LAX 22:46 · JFK 01:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.