Can not use command line interpreter

2019-03-18 03:19发布

I have tried to execute simple php code in the php interpreter. When I executed the command php -a I getting the message

Interactive mode enabled

Without any place for php input. But I can execute a php code through the command php -r. for example:

php -r "echo 'Hello stackoverflow!';"

Hello stackoverflow!

9条回答
放我归山
2楼-- · 2019-03-18 03:47

Because module readline not installed. http://php.net/manual/en/features.commandline.interactive.php

This is how I install the module by recompiling php source codes:

Find previous Configure command:

$ php -i | grep configure
Configure Command =>  './configure'  '--prefix=/usr/local/php7' ...

Then recompile:

./configure --prefix=/usr/local/php7 \
--with-readline \

...

$ make clean 
$ make
$ make test 
$ sudo make install

Check if readline module installed:

$ php m | grep readline
readline

Then start php Interactive shell:

$ php -a
Interactive shell

php >
查看更多
手持菜刀,她持情操
3楼-- · 2019-03-18 03:48

You are in interactive mode, but without a prompt, since you may not have readline mode available. You just just need to start typing, and your commands will be evaluated after you press enter. It doesn't look like anything is going on, but if you enter, for example:

<?php

echo "hello world";

?>

...you will get output...

If you enter braced blocks, they get evaluated after you press enter following the closing }

<?php 
for ($i = 0; $i < 5; $i++) {
  echo $i;
}
// prints 12345 after closing }

Note that you must start with <?php or anything entered won't be evaluated.

Update (years later):

On a Red Hat (RHEL5) system running the vendor's security patched PHP 5.3.3, I have encountered an interactive mode which did not echo back following closing braces.

Instead, the output buffer was not flushed until I pressed Ctrld. Effectively, this makes the interactive session one-time-use. Insert all code input, and Ctrld to return all output at once.

查看更多
做个烂人
4楼-- · 2019-03-18 03:59

If you're using Mac, then install Homebrew (http://brew.sh) then type: brew install phpsh

And then you can run phpsh to get an interactive shell.

查看更多
登录 后发表回答