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";
Hello
php > function test() {
php { echo "Hello";
php { }
php > test();
Hello
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
readlineextension is available with package names such asphp-readlineorphp8.1-readline. - On standard Windows builds, the
readlineextension is enabled by default. It is linked statically, and cannot be disabled. - When compiling PHP, make sure to enable the
readlineextension because it is not enabled by default.- Set the
--with-readlineoption to enablereadlinelibrary. Requireslibreadline-devon Ubuntu/Debian, orlibreadline-develon CentOS-alike systems. - Alternately, it is possible to use
libeditwith the--with-libeditoption, provided the underlyinglibedit-dev/libedit-develdependencies are installed. - Setting
--with-readline=sharedcompiles thereadlineextension to a separate shared extension with namereadline.so. Once compiled as shared extension, it must be enabled from aphp.inifileextension=readline.so
- Set the
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.