More PHP power on the command line

If you use PHP on the command line you most probably know the -r parameter to execute one line of code. This feature is quite nice but it’s hard to keep the quoting right. Depending on which quotes (single or double) and shell you are using you might even need to keep attention on escaping variables. Writing the code directly to the STDIN of a php instance is quite annoying if you just want to change something after typing it. Always writing the code into a file (even so it’s just a single line + <?php) and running this file is quite circumstantial. To solve this problem Marcus and I were adding a new feature to PHP 5.1’s CLI SAPI whicht I’d like to introduce here: An interactive PHP console.

After compiling PHP using GNU readline (–with-readline) or BSD’s libedit (–with-libedit) you can invoke the console by starting PHP with the -a flag. This will give you a nice prompt:

$ php -a<br />Interactive mode enabled<br /><br />php >

Here you can just type some code and get it execcuted. Depending on the current context the prompt changes:

php > $a = 1;<br />php > $b = 2;<br />php > echo $a + $b;<br />3<br />php > function foo() {<br />php {     echo "foo<br />php " bar";<br />php { }<br />php > foo();<br />foo<br />bar<br />php > <br />

But this isn’t all we have there. The über-cool feature is tab-completion. As used from bash or the mysql console you just type the first few letters and by pressing the tab key you get a completion or a list of possible completions. Currently we have completion for functions, constants, class names, variables, static method calls and class constants. Currently I’m working on completion for object methods or properties.

The whole completion stuff has some limitations, for example completion of variables or objects only works if it had been definied in a prevoius line already executed, not if a variable is used multiple times in the same line or inside a function. But in many cases it can save you typing unnecessary letters.

Feel free to test a current snapshot and to give us some feedback.

–johannes

18 Kommentare

  1. David Sklar wrote a utility called ‚phpcmd‘ that he included in his PHP Cookbook that does similarly, only using the readline library and eval(). I very much like the idea of having this functionality native to PHP, though — I’ve found that it makes it easy to test commands and libraries in an interactive way, and even to process a few PHP routines on the fly.

  2. First of all let me reiterate that if you’re not taking advantage of the PEAR commandline installer you’re missing out on one of the best aspects of PHP.Now, thanks to Tobias, using PEAR at the commandline has become easier than every with auto-complete e

  3. I can see that this will be useful when you’re testing a few lines of code. I use php extensively on the command line and writing a script using your fav‘ editor is the easiest thing to do – besides I can save my scripts and if I give them friendly names, I can always reuse the scripts.

  4. nice. but as the solutions written in plain php, even your patch to php isn’t able to handle a fatal error:

    interactive mode quits – and that’s really sad :-(

    1. Yes, but the problem is that after an Fatal Error the engine might be in an inconsistent state and there is no way to check this so I need to terminate.

      And there are a few benefits of this solution compared to an PHP based one: Since I have acces to the internal data strucutres I can for example offer tab-complition on different Symbol-names, partly even context-sensitive and that makes life really nice.

  5. You might know the great PHP shell mode we introduced with PHP 5.1. Fedora users can know profit from it using php-cli-5.1.6-3.3.fc6 which is the current stable release. For more information on that great shell mode read my blog post in the other blog.

  6. Due to a recent discussion on IRC I just found out that it’s now exactly three years since I first wrote a mail, including a patch, to the PHP internals list. The patch tried to use STDERR instead of STDOUT for error messages with PHP CLI. As you can se

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert