PHPSH: Test PHP Snippet, The Python Way!

If you know python, maybe you know the interactive python shell, too. If you don’t know both, well, let me explain it. Python has it’s built in interactive shell. When you install python and you execute the command, python, you will be introduced to a shell like interface where you could type python statements and have it evaluated and shown the result, right away. This is really convenience for learning purpose, or some simple snippet testing.

PHP does have this feature. If you try to lauch the php-cli with -a option (
php -a), you’ll landed on php’s interactive shell. However, it does not catch php error like python does. It will exit the shell when error encountered. This kind of behaviour limit the interactive ability. Imagine this, you’ve typed several line of codes you read on some tutorial site, however, right at the end of the line, you miss type some code and it raised error. This will force you to write all the script back from the first line. PHP does have another feature for snippet testing (beside writing to file and execute it). By using -r option, you can type the code all in one line and run it using this command: php -r "some php code". But, this one have it’s own drawback. You will have to escape every quote you have, because it will be parsed by the main shell, before parsed by php.

Luckily, guys at facebook has invented phpsh. An interactive php shell similar to python. Here’s the description according to it’s website:

phpsh is an interactive shell for php that features readline history, tab completion, quick access to documentation. It was developed at Facebook and ironically, is written mostly in python. It is open source and released under a modified BSD license.

It also able to catch error. Therefore, if you encounter it, you will be able to fix the code and run it again, interactively. Cool isn’t it!

Author: Arief Bayu Purwanto

Hello, my name is Arief Bayu Purwanto, a 24 years old father of a beautiful daughter. Interested in online programming, linux, games, and reading. Currently working on kapanlagi.com as junior developer. I live in a relatively quite and cool place called Malang. I'm available for some freelance stuff as well as some consulting job. You can see my portofolio for some previous task I've finished and some other information related to my capability. Btw, I'm plurking here.