• 请不要在回答技术问题时复制粘贴 AI 生成的内容
alwayshere
V2EX  ›  程序员

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

  •  
  •   alwayshere · Jun 1, 2018 · 4908 views
    This topic created in 3083 days ago, the information mentioned may be changed or developed.

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

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

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

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

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

    如果还不满意 想 debug。


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

    +1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4153 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 01:55 · PVG 09:55 · LAX 17:55 · JFK 20:55
    ♥ Do have faith in what you're doing.