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
readline
extension is available with package names such asphp-readline
orphp8.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 enablereadline
library. Requireslibreadline-dev
on Ubuntu/Debian, orlibreadline-devel
on CentOS-alike systems. - Alternately, it is possible to use
libedit
with the--with-libedit
option, provided the underlyinglibedit-dev
/libedit-devel
dependencies are installed. - Setting
--with-readline=shared
compiles thereadline
extension to a separate shared extension with namereadline.so
. Once compiled as shared extension, it must be enabled from aphp.ini
fileextension=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.