简体   繁体   中英

Running php script from shell does not echo anything

I have a very simple test.php file:

<?php

echo 'Hello World';

Then I run it from Mac Shell: php test.php But it does not echo anything. Looks like it's blocked for some reason, like this:

从外壳运行php脚本无显示

If I use invalid syntax in the file, for example:

<?php

invalid syntax

Then I can see the error output:

PHP Parse error:  syntax error, unexpected 'syntax' (T_STRING) in .../test.php on line 3

Parse error: syntax error, unexpected 'syntax' (T_STRING) in .../test.php on line 3

What's wrong with it?

FYI:

  • Running php -i can print php.ini info
  • Running echo 'test' can print 'test'

Turning my comments into an answer as requested :

Here are two possible reasons why php command doesn't work as expected:

  1. Maybe it is aliased (try which php or prepend a backslash (\\) to the command)

  2. You mentioned that you had a directory named php on your working directory. Well, zsh has a feature called AUTO_CD that may have interfered in this case. Basically AUTO_CD allows you to switch directories without typing cd and can be disabled by adding unsetopt autocd to file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM