PHP 8.1: CLI: Interactive shell (php -a) requires readline extension


The readline extension in PHP enables shell features such as editing, navigating, autocompletion, and more. readline is a PHP bundled extension, but it is not enabled by default at the default compile script.

PHP CLI provides an interactive shell with a -a command-line option.

php -a

Interactive shell

php >
php > echo "Hello";
php > function test() {
php { echo "Hello";
php { }
php > test();

The interactive shell cannot work without the readline extension, but prior to PHP 8.1, it was erroneously allowed to open the shell with the php -a option. The interactive features were not enabled, and it was same as opening a non-interactive shell with the php command.

In PHP 8.1 CLI, the interactive shell (php -) exits with an error code if the readline extension is not enabled.

php -a
Interactive shell (-a) requires the readline extension.

Further, the php --help command also mentions this:

php --help
Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]

  -a    Run as interactive shell (requires readline extension)

Enable/Compile readline extension

  • If pre-compiled PHP packages are installed on Linux systems, the readline extension is available with package names such as php-readline or php8.1-readline.
  • On standard Windows builds, the readline extension is enabled by default. It is linked statically, and cannot be disabled.
  • When compiling PHP, make sure to enable the readline extension because it is not enabled by default.
    • Set the --with-readline option to enable readline library. Requires libreadline-dev on Ubuntu/Debian, or libreadline-devel on CentOS-alike systems.
    • Alternately, it is possible to use libedit with the --with-libedit option, provided the underlying libedit-dev/libedit-devel dependencies are installed.
    • Setting --with-readline=shared compiles the readline extension to a separate shared extension with name Once compiled as shared extension, it must be enabled from a php.ini file

Backwards Compatibility Impact

The PHP CLI interactive shell was not usable without the readline extension in all PHP versions. In PHP 8.1, the interactive shell quits with a message Interactive shell (-a) requires the readline extension. if the readline extension is not available.

Enable readline extension to enable the interactive shell.

Discussion Implementation