Stefan Fischerländer’s Blog One Blog Is Not Enough

phpa-norl – PHP shell for Mac OS X and Windows

Update! Thanks for alle the nice and helpful comments. I’ve integrated the hints concerning split, register_shutdown_function and PHP_EOL. (2010/06/16)

When writing code I really like to have some kind of fast feedback system, like the Smalltalk workspace or the Python IDLE shell. For Perl development I have created a graphical Perl shell for that purpose.

Because I now have to do more and more work with PHP, I’ve been looking for a similiar tool and came across phpa, “an interactive command line shell for PHP” from David Phillips. Unfortunately phpa needs readline which isn’t available on Mac OS X and Windows. (No, of course readline is available, but you have to compile PHP yourself to use it.)

So, I re-worked David’s PHP shell and removed all the fancy readline stuff. Without readline support the shell isn’t as useful as it could be, but at least it now runs on Mac OS X and I think it will also run on Windows systems. This interactive php shell without readline support I call phpa-norl – where the norl part obviously stands for “no readline” support.

screenshot: PHP shell phpa-norl

Installation

Download the PHP shell file phpa-norl and start it with php phpa-norl.

To mimic the readline support, there is a simple history function in phpa-norl. Entering a single ‘h’ lists the twenty last commands you executed. Appending a number to ‘h’, executes the command with that number in the history list. ‘h0′ for example always re-executes your last command. You can also enter a multi-line statement. If the last character on your input line is a ‘#’, than phpa-norl assumes that there will follow additional code on a new line. To leave the PHP shell, just enter a simple ‘q’ and phpa-norl will exit.

In Windows, I use a phpa.bat file containing just a single line:

php c:\php\phpa.php

Saving this phpa.bat in a directory within your PATH, you can invoke the shell simply by:

phpa

PHP shell usage

Here is a typical phpa-norl session:

macbook:~ sf$ ./phpa-norl
PHP 5.2.0 (cli) (???) [Darwin]
>>> $a=array(17,899,324,234)
>>> $b=array(288,900,1212,2323)
>>> $c=array_merge($a,$b)
>>> sort($c,SORT_NUMERIC)
true
>>> print_r($c)
Array
(
    [0] => 17
    [1] => 234
    [2] => 288
    [3] => 324
    [4] => 899
    [5] => 900
    [6] => 1212
    [7] => 2323
)
>>> $a[]=199
>>> h
History:
[5] => $a=array(17,899,324,234)
[4] => $b=array(288,900,1212,2323)
[3] => $c=array_merge($a,$b)
[2] => sort($c,SORT_NUMERIC)
[1] => print_r($c)
[0] => $a[]=199
>>> h3
>>> $c=array_merge($a,$b)
>>> h
History:
[5] => $a=array(17,899,324,234)
[4] => $b=array(288,900,1212,2323)
[3] => sort($c,SORT_NUMERIC)
[2] => print_r($c)
[1] => $a[]=199
[0] => $c=array_merge($a,$b)
>>> h3
>>> sort($c,SORT_NUMERIC)
true
>>> h
History:
[5] => $a=array(17,899,324,234)
[4] => $b=array(288,900,1212,2323)
[3] => print_r($c)
[2] => $a[]=199
[1] => $c=array_merge($a,$b)
[0] => sort($c,SORT_NUMERIC)
>>> h3
>>> print_r($c)
Array
(
    [0] => 17
    [1] => 199
    [2] => 234
    [3] => 288
    [4] => 324
    [5] => 899
    [6] => 900
    [7] => 1212
    [8] => 2323
)
>>>

Update:
Since PHP 5.1 there is (at last!) a built-in interactive mode as Johannes Schlüter shows in his blog, but this interactive mode needs readline support which isn’t part of the official Windows or Mac OS X builds. phpa-norl addresses anyone who doesn’t like to compile PHP.

Update:
Licence: phpa-norl is, like the original phpa, public domain. Feel free to do with it whatever you want.

36 Responses to “phpa-norl – PHP shell for Mac OS X and Windows”

  1. developercast.com » Christian Wenz’s Blog: phpa-norl, a phpa port for Mac OS X and Windows says

    [...] a patched version of the interactive command line PHP client, phpa to work on a Mac OS X machine – phpa-norl. phpa is an interactive command line shell for PHP by David Phillips. Stefan Fischerlander, [...]

  2. OmgSaveMe » Blog Archive » phpa-norl, a phpa port for Mac OS X and Windows says

    [...] Windows (the default builds theredo not seem to support a feature required by phpa). The result: phpa-norl. Stefan successfully ran this on OS X, and I could confirm that it runs on Windows, as [...]

  3. e-satis says

    Works great under Win XP and Ubuntu Feisty, thanks a lot ! That’s what I was looking for since a long time cause it’s one of the features I miss from Python, and I can’t afford compiling php each time I need realines.

    I espacially appreciate that you can forget the ‘”‘ and ‘;’ for quick test and directly print vars. What’s licence is it under ? Can I share it ? Modify it ? For now, I’ll just talk about it at my office.

  4. e-satis says

    Is there any place to report some bugs ? I’ll put the first one here :

    Under Ubuntu 7.04, CTRL + D, which should semantically end the prompt, makes the script start an endless loop.

    Still, it’s perfectly usable so no worries.

  5. Stefan says

    e-satis,
    phpa-norl is public domain, so just use or modify it as you like. The original script phpa from David Phillips is also public domain.
    I can confirm the CTRL-D bug is/was happening on Mac OS X too. I have now a patched version put online which should gracefully die if you press CTRL-D.

  6. e-satis says

    That what a quick fix. It’s clean on ubuntu too. Thanks again.

  7. Daniel Klein says

    There seems to be a problem using the join() function, eg:

    P:\>php phpa.php
    PHP 5.2.5 (cli) (???) [WINNT]
    >>> $j = join(“,”, array(“php”,”is”,”great”)
    PHP Parse error: syntax error, unexpected ‘;’ in P:\phpa.php(66) : eval()’d code on line 1
    false
    >>>

  8. Stefan says

    Daniel,
    there’s missing the closing parenthesis in your sample code.

    This works for me:
    >>> $j = join( “,”, array(“php “, “is “, “great “))
    >>> $j
    ‘php ,is ,great ‘
    >>>

  9. estetik says

    Very useful information for me.

  10. otogaz says

    I know you’ve done some great work with ffmpeg – do you have any info about a utility that rearranges a Quicktime file to switch the moov atom to the front of the data, for streaming? (Tinic mentions qt-faststart.c?)

  11. Stefan says

    otogaz, I’ve never done anything with ffmpeg. Perhaps David has; he’s the author of the original phpa. The link to his site is in top part of my post.

  12. kulak estetigi says

    phpa is an interactive command line shell for PHP and phpa can use either the CGI or CLI version of PHP. This shell works flawlessly under ubuntu. Thanks a lot.

  13. Hospital says

    […] a patched version of the interactive command line PHP client, phpa to work on a Mac OS X machine – phpa-norl. phpa is an interactive command line shell for PHP by David Phillips. Stefan Fischerlander, […]

  14. çeşme otel says

    phpa-norl – PHP shell for Mac OS X and Windows great article thank you.

  15. Raubsischangeln says

    That what a quick fix. It’s clean on ubuntu too. Thanks again.

  16. duvar kağıtları says

    phpa-norl – PHP shell for Mac OS X and Windows great article thank you.

  17. stev.ie/ » Blog Archive says

    [...] Via. [...]

  18. burun estetiği says

    very great article thank you admin..

  19. Dan Sargeant says

    This script is a life saver! A great way to test something out if interactive mode does not work for you. Thanks

  20. smartov says

    On line 179 split function is used. This function is deprecated and therefore produces E_DEPRECATED warning.
    You should replace it with
    $kw = preg_split(“/[^A-Za-z0-9_]/”, $code);

    Thank you for this script.

  21. George says

    phpa-nourl working on WIN great article thanks stefan

  22. love says

    because split() is deprecated as of php 5.3.0,please replace $kw = split(“[^A-Za-z0-9_]“, $code);
    with
    $kw = preg_split(“[^A-Za-z0-9_]“, $code);

  23. Chris B says

    Having a great time with php-norl. It’s completely replaced my previous PHP test file that I used to use and then run via the console. If you plan on updating it, I would like to see better handling of errors rather than dumping back to the command prompt.

    Also, in your method for grabbing the build dtae from phpinfo you reference a constant named LINE_BREAK, but I see no definition for it and it isn’t built-in to PHP. I wonder if you meant to use PHP_EOL instead?

  24. slier says

    im sorry..
    im noob
    how to use it in windows
    i download it…
    but cant use it

  25. gwbasic32 says

    Added error message on eval() error:

    Put this at the top.

    register_shutdown_function(‘shutdown’);

    function shutdown()
    {
    // This is our shutdown function, in
    // here we can do any last operations
    // before the script is complete.

    global $gEvalError, $gErrorScript;

    if ( $gEvalError ) {
    echo PHP_EOL;
    echo PHP_EOL;
    echo ‘eval() error: ‘ . $gErrorScript, PHP_EOL;

    //$last_line = system(‘php shell.php’, $retval);
    }
    }

  26. sudo says

    Great! Thank you very much!
    I use this program under CentOS with php5.3 (because there’s no rpm packages php-readline for 5.3) and it works fine.

  27. saiwingy says

    Awesome script! I don’t know how I programmed php before this…

    I have a feature request though. Can we get up/down arrow keys for history? h# is nice but arrow keys are much more convenient for me…

  28.   Интерактивный режим PHP или PHP-шелл [Within: блог samlowry] says

    [...] нашлось: бодрый парень Stefan Fischerländer написал скриптик phpa-norl, который делает всё то же самое, что и ‘php -a’, [...]

  29. Peter says

    Thank you! Works great on WinXP and php 5.3

  30. Morten says

    Im sorry, but this is awful on windows 7 php 5.3, returns false to any statement or simply exits the terminal. couldnt even get it to return function test($x){return x+2;} properly, hope you make this work sometime!

  31. Run PHP interactive mode in Mac OS X… « SysAdmin by chance says

    [...] mode without recompiling PHP. The script, phpa-norl (php -a noreadline) is available in his website http://www.fischerlaender.net/php/phpa-norl . Download it now, it would make your life [...]

  32. karın estetiği says

    Awesome script! I don’t know how I programmed php before this…
    I have a feature request though. Can we get up/down arrow keys for history? h# is nice but arrow keys are much more convenient for me…

  33. Stefanos Grammenos says

    I would like you to consider this version of myReadLine that supports 2 types of prompt for multi line editing with comments included. Thanks

    function myReadLine($fh, $prompt, $prompt2=’… ‘){
    echo $prompt;
    $complete_line = ”;
    $preg_remove = array(
    “/([^\"']*)(‘[^']*’|\”[^\"]*\”)/”,
    ‘|//.*?$|m’,
    ‘|#.*?$|m’,
    ‘|/\*.*?\*/|s’,
    );
    for(;;) {
    $line = fgets($fh,1024);
    if( !$line && strlen($line)==0 ) # this is true, when CTRL-D is pressed
    die(“\nUser pressed CTRL-D. phpa-norl quits.\n”);
    $complete_line .= $line;
    $cl = str_replace(array(‘\\\\’, ‘\”‘, “\’”, ‘\/’,), ”, $complete_line);
    $cl = preg_replace($preg_remove, ‘$1′, $cl);
    $chrs = count_chars($cl);
    $chr = ”;
    switch(true){
    case substr_count($cl, ‘/*’) > 0:
    $chr = ‘[/*]‘; break;
    case $chrs[ord("'")] %2 > 0:
    $chr = “['] “; break;
    case $chrs[ord('"')] %2 > 0:
    $chr = ‘["] ‘; break;
    case $chrs[ord('{')] – $chrs[ord('}')] > 0:
    $chr = ‘[{] ‘; break;
    case $chrs[ord('(')] – $chrs[ord(')')] > 0:
    $chr = ‘[(] ‘; break;
    default:
    break 2;
    }
    echo $chr.$prompt2;
    // echo $cl . ‘ ‘;
    }
    return trim($complete_line);
    }

  34. David Luu says

    First off, great tool and article! Useful for me to do debugging of Selenium RC commands for PHPUnit on Windows. But wanted to mention this feedback: haven’t tried phpa but I’ve used the Python shell and what I miss from that that isn’t here with phpa-norl is exception handling. With Python shell, from what I’ve used of it so far, exceptions are caught and dumped as output in shell and you can proceed with the shell as you please with current variables/objects still in memory. With this phpa-norl, minor issues like some parser errors are dumped to shell and you can continue but fatal exceptions crash the shell and you have to start over with new shell, and existing variables/objects lost. I’d have to do everything over again. :( Be nice if you could find a way to incorporate full exception handling (as an option if not always on) so that we don’t get a shell crash and can continue with our work.

  35. Hosh says

    Hi,

    So I was using php-norl using php 5.3.5 on Windows 7, and I noticed that the build date wasn’t being read properly with PHP_EOL (strange, this should work), so I replaced the line explode(PHP_EOL, $x); with the following:

    if(preg_match(“/\r\n/s”, $x)) {
    $x = explode(“\r\n”, $x);
    } else if(preg_match(“/\r/s”, $x)) {
    $x = explode(“\r”, $x);
    } else if(preg_match(“/\n/s”, $x)) {
    $x = explode(“\n”, $x);
    }

    Simple and easy and works!

    Thank you and David for this awesome script! It’s a shame PHP doesn’t support interactive shell properly on Windows.

  36. error 651 says

    error 651…

    [...]phpa-norl – PHP shell for Mac OS X and Windows[...]…

Leave a Reply