PHP interactive shell, auto echo and a new line

1.4k views Asked by At

Greetings fellow programmers!

Is there a way to get PHPs interactive shell, php -a, to behave more like Rails console or the console in Chrome? I have looked through the flags for the php-command, but no dice.

What I get:

php > $a = 0;
php > $a;
php > echo $a;
0php > 

What I want

php > $a = 0;
0
php > $a;
0
php > echo $a;
0
php > 
3

There are 3 answers

0
Alexander Yancharuk On

You have at least three possibilities to solve this problem:

  1. Manually add PHP_EOL to all your echo: echo $a . PHP_EOL;
  2. Introduce your idea on official php ideas wiki and wait until someone implement it.
  3. Learn php git workflow for external contributors, create needed functionality and send the patch to developers.
0
psychoslave On

As of 2022, PsySH seems to be a modern maintained valid solution for the asked question:

Psy Shell v0.11.5 (PHP 7.4.3 — cli) by Justin Hileman
>>> $a = 0
=> 0

>>> $a
=> 0

>>> echo $a
0⏎

All the more:

  • as one can see above, semi-colon is optional
  • the output is slightly different for an echo expression
  • a single eval statement dropped anywhere in a code base is enough to set a breakpoint powered by this REPL
0
rymo On

Check into Boris, described as "PHP's missing REPL". echo and print still require manual newlines, but naked expressions are evaluated and pretty-printed:

[1] boris> $a = 0;
// 0
[2] boris> $a;
// 0
[3] boris> echo $a;
0[4] boris>