Interactive Shell Using PHP

How to use the PHP interactive shell

Try installing http://www.phpsh.org/ it is probably the easiest solution.

Steps: (assuming dependency's installed)

  1. git clone https://github.com/facebook/phpsh
  2. cd phpsh
  3. sudo python setup.py install
  4. phpsh

How to execute in PHP interactive mode

Ctrl+d will trigger an end-of-file condition and cause the script to be executed.

See also How does the interactive php shell work?

Interactive Shell and Interactive Mode are not the same thing, despite
the similar names and functionality.


If you type php -a and get a response of 'Interactive Shell' followed by a php> prompt, you have interactive shell available (PHP
was compiled with readline support). If instead you get a response of
'Interactive mode enabled', you DO NOT have interactive shell
available and this article does not apply to you.


So if you get only "Interactive mode enabled", then you'll only be
able to type in PHP code and then when you're done, send PHP an EOF to
execute it.

"EOF" means "end-of-file".

How does the interactive PHP shell work?

On the documentation for the interactive shell, the first note by Ryan P. has some notable information:

Interactive Shell and Interactive Mode are not the same thing, despite the similar names and functionality.

If you type php -a and get a response of 'Interactive Shell' followed by a php> prompt, you have interactive shell available (PHP was compiled with readline support). If instead you get a response of 'Interactive mode enabled', you DO NOT have interactive shell available and this article does not apply to you.

So if you get only "Interactive mode enabled", then you'll only be able to type in PHP code and then when you're done, send PHP an EOF to execute it.

This is probably not what you want. You may want to look into phpsh instead.

Interactive shell using PHP

Yes, it's possible. In order to be interactive, the program must be able to wait for and read in user input from stdin. In PHP, you can read from stdin by opening a file descriptor to 'php://stdin'. Taken from an answer to different question, here's an example of an interactive user prompt in PHP (when run from the command line, of course):

echo "Continue? (Y/N) - ";

$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if ($response != 'Y') {
echo "Aborted.\n";
exit;
}

Of course, to get a full line of input rather than a single character, you'd need fgets() instead of fgetc(). Depending what your program/shell will do, the whole program might be structured as one big continuous loop. Hopefully that gives you an idea how to get started. If you wanted to get really fancy (CLI pseudo-GUI), you could use ncurses.

How to run php in interactive CLI in Windows

For a powerful interactive PHP CLI, I suggest you take a look at PsySH. It's very easy to install, you just have to run one command:

composer g require psy/psysh:@stable

Then make sure you have added the composer vendor directory to the PATH system variable (instructions) by appending the following:

;C:\Users\[username]\AppData\Roaming\Composer\vendor\bin\

Then start it just run:

psysh

If you want an even console better experience, I suggest you use cmder instead of the Windows Command Prompt.

initialising PHP interactive

As Tomas Creemers mentioned, you have to use auto_prepend_file PHP flag to auto-require a file. For example:

<?php
# foo.php
function bar() { print "Bar.\n"; }

You can load PHP interpreter like this:

[hron@merlin tmp ] $ php -d auto_prepend_file=$PWD/foo.php -a
Interactive shell

php > bar();
Bar.
php >

Or you can include file manually:

[hron@merlin tmp ] $ php -a
Interactive shell

php > include 'foo.php';
php > bar();
Bar.
php >

How do I write a command-line interactive PHP script?

From PHP: Read from Keyboard – Get User Input from Keyboard Console by Typing:

You need a special file: php://stdin which stands for the standard input.

print "Type your message. Type '.' on a line by itself when you're done.\n";

$fp = fopen('php://stdin', 'r');
$last_line = false;
$message = '';
while (!$last_line) {
$next_line = fgets($fp, 1024); // read the special file to get the user input from keyboard
if (".\n" == $next_line) {
$last_line = true;
} else {
$message .= $next_line;
}
}


Related Topics



Leave a reply



Submit