Interactive shellThe CLI SAPI provides an interactive shell using the -a option if PHP is compiled with the --with-readline option. As of PHP 7.1.0 the interactive shell is also available on Windows, if the readline extension is enabled. Using the interactive shell you are able to type PHP code and have it executed directly. Example #1 Executing code using the interactive shell $ php -a Interactive shell php > echo 5+8; 13 php > function addTwo($n) php > { php { return $n + 2; php { } php > var_dump(addtwo(2)); int(4) php > The interactive shell also features tab completion for functions, constants, class names, variables, static method calls and class constants. Example #2 Tab completion Pressing the tab key twice when there are multiple possible completions will result in a list of these completions: php > strp[TAB][TAB] strpbrk strpos strptime php > strp When there is only one possible completion, pressing tab once will complete the rest on the same line: php > strpt[TAB]ime( Completion will also work for names that have been defined during the current interactive shell session: php > $fooThisIsAReallyLongVariableName = 42; php > $foo[TAB]ThisIsAReallyLongVariableName The interactive shell stores your history which can be accessed using the up and down keys. The history is saved in the ~/.php_history file.
The CLI SAPI provides
the php.ini settings It is also possible to set php.ini settings in the interactive shell using a shorthand notation. Example #3 Setting php.ini settings in the interactive shell
The php > #cli.prompt=hello world :> hello world :> Using backticks it is possible to have PHP code executed in the prompt: php > #cli.prompt=`echo date('H:i:s');` php > 15:49:35 php > echo 'hi'; hi 15:49:43 php > sleep(2); 15:49:45 php > Setting the pager to less: php > #cli.pager=less php > phpinfo(); (output displayed in less) php >
The
Interactive modeIf the readline extension is not available, prior to PHP 8.1.0, invoking the CLI SAPI with the -a option provided the interactive mode. In this mode, a complete PHP script is supposed to be given via STDIN, and after termination with CTRL D (POSIX) or CTRL Z followed by ENTER (Windows), this script is evaluated. This is basically the same as invoking the CLI SAPI without the -a option. As of PHP 8.1.0, invoking the CLI SAPI with the -a option fails, if the readline extension is not available. |