I was first introduced to PsySH back at OSCON last year. While I still haven’t used it much as a debugger, I’ve found that a REPL is a handy tool to have around.

A PHP what?

PsySH implements a Read-Eval-Print-Loop, or REPL, for PHP. This might sound simple, but a REPL Reads input, Evaluates that input, Prints it to the console, then Loops back to the read step. What’s that look like, you ask? Let’s see.

>>> $foo = "Hello"
=> "Hello"
>>> $bar = "World"
=> "World"
>>> $foo . $bar
=> "HelloWorld"

Notice that after every input line, we get a corresponding output. The program is reading our input ($foo = “Hello”), evaluating it (this is an assignment, so it’s straightforward), printing it out, then prompting for input again.

Wait, We Already Have one of Those

“But wait,” I hear you say, “PHP already has an interactive mode!” True, php -a exists, but have you actually used it? It skips the Print part of that whole REPL thing, it has a tendency to crash if you accidentally have a PHP fatal error, and it doesn’t really do much aside from giving you access to a PHP interpreter.

“But wait,” I hear you say, “Facebook already did that!” Yes, phpsh is a thing that facebook did, but they seem to have forgotten about it (last updated three years ago at the time of this writing) and it has this inconvenient dependency on Python. As in, it’s written in Python, and you need to have Python available in your environment to use it. Hmm.

OK, but why?

Remember the bad old days before in-browser css inspectors? Styling elements was a major pain, right? Edit your stylesheet, save, refresh the page in the browser. Hmm, that div still doesn’t look right, let me try again, edit, save, refresh, ad nauseum. Now, thanks to the miracle of property inspectors and live style editors, we can fiddle with css directly in the browser, get real-time feedback, and save it down to our stylesheets only when we’re satisfied with the result.

So why wouldn’t you want to write all your code like this? Turns out you can write functions in the interactive shell as well:

>>> function addThese($a, $b) {
... $result = $a + $b;
... return $result;
... }
=> null
>>> addThese(5, 6);
=> 11

Forgot what arguments array_push() takes? No problem, just install the PHP manual and you can look it up right from the shell:

>>> doc array_push
function array_push(&$stack, $var, $... = unknown)

Push one or more elements onto the end of array

array_push treats array as a stack, and pushes the passed variables onto the end of array.
The length of array increases by the number of variables pushed. Has the same effect as:
]]> repeated for each var.

array $array The input array.
mixed $var The pushed value.

int Returns the new number of elements in the array.

OK, fine, how do I use it?

I’m glad you asked! PsySH is written in PHP and is available as a precompiled phar. If you just want to use it as a REPL, it’s as easy as:

chmod +x psysh

PsySH is still a young product (currently version 0.1.0) but already a useful tool. If it looks interesting, check it out on GitHub and consider contributing issues and code if you wind up finding it useful.

Leave a Reply

Your email address will not be published. Required fields are marked *