REPL for PHP - Part 1 : Built-in Interative Shell

A Read-eval-print loop (REPL) is a simple and quick programming environment for testing short code snippet or debugging. In PHP, there are five REPLs namely PHP's native interactive shell, Boris, Tiny PHP REPL, PHPSH, and Wigwam. Let's go through each one of them.

First, let's start with the default built-in REPL environment which was available since PHP 5.1. For interactive mode (you can scroll back each line of code through history logging in ~/.php_history), just make sure the readline extension is enabled.
$ php -m | grep readline

$ php -a
Interactive shell
php >

One of the nicest feature is auto-completion (just type the tab key), extremely useful if you can't remember the exact functions or classes name.
php > print easter_da
easter_days  easter_date

Another useful feature is setting your viewing pager. Useful when you're expecting long result that spans multiple screens. Unfortunately, this setting is only available from PHP 5.4 onwards.
php >   .pager=less
php > phpinfo();

Unfortunately, there are two limitations. First, there is no syntax highlighting support thus making reading the code or result a bit difficulty. Second, you have to manually print out result of every evaluated expression, which is repetitious and troublesome.
php > print_r(new DateTime());
DateTime Object
    [date] => 2013-10-13 01:49:44
    [timezone_type] => 3
    [timezone] => Asia/Kuala_Lumpur

No comments:

Post a Comment