PHP CLI 模式下,怎么随时调试打印出各个变量值? - V2EX
请不要在回答技术问题时复制粘贴 AI 生成的内容
alwayshere

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

  •  
  •   alwayshere Jun 1, 2018 4520 views
    This topic created in 2908 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
        1
    zjsxwc  
       Jun 1, 2018
    做成交互界面呗,比如用 ncurses http://php.net/manual/en/intro.ncurses.php
    OMGZui div class="fr">     2
    OMGZui  
       Jun 1, 2018
    只是想看爬到哪个页面和第几页,比如 for 循环,打印出 url 和 i 不就行了。
    zhengwenk
        3
    zhengwenk  
       Jun 1, 2018
    echo $demo."\n"
    wangysong
        4
    wangysong  
       Jun 1, 2018   1
    error_log(print_r($data,ture));

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

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

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

    如果还不满意 想 debug。


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

    +1
    About     Help     Advertise     Blog     API     FAQ     Solana     2994 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 61ms UTC 12:49 PVG 20:49 LAX 05:49 JFK 08:49
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86