2

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 > 
Emil
  • 1,035
  • 3
  • 10
  • 19
  • There's [Facebook's `phpsh`](https://github.com/facebook/phpsh), but it looks like their original fork isn't maintained anymore. It worked a while ago. – Blender Dec 17 '13 at 08:13
  • You're asking for broken functionality. It behaves exactly as it should, however if you want a newline - why not add it yourself? It's really not hard to use `printf("\n%s", $a);` instead of `echo $a;` to accommodate for CLI usage. – N.B. Dec 17 '13 at 10:30
  • 2
    Maybe you see it as broken but I would see it as a feature. The point (to what I understand) the interactive shell is about easy access to php, for testing or something similar. Trying out your class or something. It would be a hell of a lot more convenient and easy to use if it behaved as I proposed. – Emil Dec 18 '13 at 20:36

3 Answers3

2

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.
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
1

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
psychoslave
  • 2,783
  • 3
  • 27
  • 44
0

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>
rymo
  • 3,285
  • 2
  • 36
  • 40