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
Interactive mode enabled
php >
Here you can just type some code and get it execcuted. Depending on the current context the prompt changes:
php > $a = 1;
php > $b = 2;
php > echo $a + $b;
3
php > function foo() {
php { echo "foo
php " bar";
php { }
php > foo();
foo
bar
php >
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
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
Tracked: Jun 04, 12:18
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
Tracked: Nov 13, 13:31
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)
Tracked: Nov 13, 14:04
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)
Tracked: Nov 13, 17:24
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.
Tracked: Dec 22, 00:06
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
Tracked: Jul 18, 00:04