18

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!

4
  • This looks like a bug; see bugs.php.net/bug.php?id=48759 . You could try phpsh instead. Commented Apr 23, 2012 at 15:16
  • 1
    @JoeyAdams this is not a bug but a user error, which is also documented in this bug report. proper make clean solved it. Commented Apr 23, 2012 at 15:21
  • 2
    btw, the fact that you are on Windows is an important detail, because readline is a gnu module. Keep that in mind when trying to debug functionality. php.net/manual/en/intro.readline.php (along with the other link), basically states that "php -a" isn't available on Windows. There are other means though. Commented Apr 23, 2012 at 17:25
  • 2
    Ah yes, some obscure module that shouldn't be remotely necessary in the first place isn't available, so it fails silently without any warning or indication. Brilliant. Commented Oct 18, 2013 at 6:52

9 Answers 9

36

Install this:

php5-readline

then try use:

php -a
Sign up to request clarification or add additional context in comments.

1 Comment

Running PHP functions in cmd is really fun!
7

type php -m and make sure you have the readline module. If you don't you won't be able to use it.

http://www.php.net/manual/en/features.commandline.interactive.php

As of PHP 5.1.0, the CLI SAPI provides an interactive shell using the -a option if PHP is compiled with the --with-readline option.

3 Comments

Now I know why I cant use interactive mode properly. I am on Windows operation system and the readline module cant be installed on it. So my solution is move to linux(Can I use the cli purely?)?
@user1341970 See my answer - you can use interactive mode on Windows, it just doesn't give you a prompt. You have to type into the terminal under interactive mode.
@user1341970 I don't see why you couldn't. You can always read in input from a script if you wanted to do it that way on windows, but you are going to have a problem with readline specifically (the module, not the function).
3

There seems to be a compilation / linkage error between your PHP and libreadline. This is documented in PHP Bug #48759.

  • Did you compile PHP by yourself? Did you play around with --configure and didn't do a proper make clean before your final build?
  • Does php -m list readline as enabled feature? (Is PHP built with option --with-readline)
  • What is you php version?
  • What distribution do you use?
  • Do you have the libreadline (the *-dev package) installed?

1 Comment

The OP was using windows, so probably used precompiled binaries.
2

After I got tired of compiling it on each machine I used PHPSH (as mentioned by joey-adams)

It is much better then php -a (syntax highlighting and autocompletion)

Install python phpsh

look at install-php5-with-readline-support-on-debian-wheezy

$ sudo apt-get install python # this is necessary to run phpsh
$ cd ~/

$ wget https://github.com/facebook/phpsh/zipball/master
$ unzip phpsh-master.zip

$ cd phpsh-master
$ sudo cp -r src /etc/phpsh # phpsh seems to complain unless it resides at /etc/phpsh
$ sudo ln -s /etc/phpsh/phpsh /usr/bin/phpsh # put phpsh on the $PATH

Comments

2

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.

6 Comments

The OP is on Windows (it wasn't said), but it doesn't really do anything except shows you what you are typing on the screen (well, the machine I tested this on).
@craniumonempty It does work on windows as I describe above. I verified it before posting. Also works this way on other platforms in absense of readline.
Hmm, is there something else that needs to be installed, because it doesn't on this one (windows7). This isn't my machine, so it's on xampp from a jump drive.
@craniumonempty Did you start it with <?php? If not, it will just echo back what you input.
Yes, tried with and without. It simply shows what I type and doesn't evaluate anything.
|
1

To check if you have readline module installed, type: php -m | grep readline

If nothing displayed, install readline module: sudo apt-get install php5-readline

After module installation you can recheck it's presence with previous command, than enter interactive mode with: php -a

Comments

1

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 >

Comments

0

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.

Comments

-2

This is what you should see:

# php -a
Interactive shell

php > echo 1+1;
2
php > echo PHP_VERSION;
5.3.2-1ubuntu4.14
php > exit
#

6 Comments

possibly because he just posted the expected behaviour, which the OP doesn't seem to be able to produce on his machine. This is not an answer.
@halfer Not mine, but I assume because this question is about PHP interactive mode without readline support, meaning no php > prompt.
The original poster posted little more than 'it doesn't work'. That largely left guessing what is going on with nothing to compare against.
This is not an answer - @Kai, I politely disagree. Try to be positive about people's contributions, especially when they're from someone with excellent rep :). Edit: and especially when you have also posted an answer. Posting answers here should be collaborative, not a competition!
@halfer i agree with your opinion about being positive. I just tried to answer your question why this got downvoted and actually, "Interactive mode enabled" is good enough to compare against as it is the only message the OP receives from php -a.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.