dream_high1026 2017-01-18 00:42
浏览 98
已采纳

为什么这个PHP代码不能在Linux中使用双引号的命令行上运行

I have access to an Ubuntu server with PHP 7.0. I can run the following code without any problems:

php -r "echo 'hello world';"

But when I add an assignment operator like this:

php -r "echo 'hello world'; $t = 'hello world';"

I get the following error:

PHP Parse error: syntax error, unexpected '=', expecting end of file in Command line code

I just realized while typing this question that this is probably happening because the shell is trying to evaluate $t. Is there anyway to make it not evaluate $t? The reason I ask is because the code will contain single quotes. I suppose the simple solution would be to change the single quotes in the code to double quotes but if there is a way to do it without altering the code, it might be useful to know.

  • 写回答

2条回答

  • dongmufen8105 2017-01-18 01:11
    关注
    1. you can use single quotes for php code, but then you can't use them inside php code.
    2. escape all special shell characters in php code with \, ie:

      php -r "echo 'hello world'; \$t = 'hello world';"
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器