More PHP power on the command line

Avatar von Johannes Schlüter

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

Avatar von Johannes Schlüter

Kommentare

18 Antworten zu „More PHP power on the command line“

  1. Avatar von Lukas

    this is indeed quite cool ..
    aside from that exceptions suck ;-)

  2. Wow, that’s really cool. Something I’ve wanted for years.

  3. Works very nice, good work!

  4. That sounds fantastic. Makes total sense for an interpreted language. Great idea!

  5. 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.

  6. 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

  7. Avatar von Rishad

    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.

  8. Avatar von
    Anonymous

    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.

      1. If I remember right, the PHP version addresses this problem by using the tokenizer …

        And the tab completion is of course a must!

    2. As php -a didn’t worked for me as expected, I wrote a php-based solution which is handling most FATAL errors by parsing the PHP before executing it:

      http://jan.kneschke.de/projects/php-shell/

  9. Here’s a tidied up excerpt of the current NEWS file with all relevant changes since PHP 5.0, which may help on the decision to upgrade (even from PHP 4 )

    Fixes

    More than 350

    Changes

    Changed SQLite extension to be a shared module in Windows

  10. Here’s a tidied up excerpt of the current NEWS file with all relevant changes since PHP 5.0, which may help on the decision to upgrade (even from PHP 4 )

    Fixes

    More than 350

    Changes

    Changed PDO constants to class constants (PDO::CONST_NAME)

  11. Here’s a tidied up excerpt of the current NEWS file with all relevant changes since PHP 5.0, which may help on the decision to upgrade (even from PHP 4 )

    Fixes

    More than 350

    Changes

    Changed PDO constants to class constants (PDO::CONST_NAME)

  12. 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.

  13. 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

  14. Very cool feature, but didn’t work for me yet as Debian seems to have bugs in the readline package.

Schreibe einen Kommentar

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


Für das Handling unseres Newsletters nutzen wir den Dienst HubSpot. Mehr Informationen, insbesondere auch zu Deinem Widerrufsrecht, kannst Du jederzeit unserer Datenschutzerklärung entnehmen.