duanjuelian4640 2014-07-24 15:55
浏览 66
已采纳

PHP CLI可以在终端窗口中立即返回结果吗?

I'm trying to understand how the php cli is supposed to work. Do you have to run everything as a script like:

Filename: helloworld.php

<?php
print "Hello World!";
?>

followed by:

chmod +x helloworld.php
php helloworld.php

Or is there a way to have it interact the way python does?

I tried using the -a option but am still not getting any results.

  • 写回答

1条回答 默认 最新

  • dongtiandexue123456 2014-07-24 16:11
    关注

    I can't speak Python but I suppose you're looking for an interactive console as, for example, the one found in many JavaScript platforms such as Node:

    C:\>node
    > var x=2, y=3;
    undefined
    > console.log(x*y);
    6
    undefined
    >
    

    As far as I know, PHP does not have such feature. The PHP interpreter needs to parse the complete script before it's able to execute it.

    There is an interactive mode but it expects a single code block—it isn't a console:

    C:\>php -a
    Interactive mode enabled
    
    C:\>php -a
    Interactive mode enabled
    
    <?php
    $x=2;
    $y=3;
    ^Z
    
    C:\>php -a
    Interactive mode enabled
    
    <?php
    echo $x*$y;
    ^Z
    
    Notice: Undefined variable: y in - on line 2
    
    Call Stack:
        7.7505     355096   1. {main}() -:0
    
    
    Notice: Undefined variable: x in - on line 2
    
    Call Stack:
        7.7505     355096   1. {main}() -:0
    
    0
    C:\>
    

    I'd like to be able to input true == false; and see the result.

    The closest you can get is -r:

    C:\>php -r "var_dump(true == false);"
    bool(false)
    

    Don't forget shell escaping rules.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?