phpa

What is it?

phpa is an interactive command line shell for PHP.

It is a replacement for the interactive mode of the PHP interpreter (php -a), hence the name.

This software is public domain.

How to install phpa

Download phpa. You may want to view the highlighted source code.

Rename it to phpa and make it executable:

mv phpa.txt phpa
chmod a+x phpa

The first line of phpa may need to be modified to match the path to the PHP interpreter on your system. phpa can use either the CGI or CLI version of PHP.

Optionally, move phpa to a directory that is in your path, such as your personal bin directory (~/bin) or /usr/local/bin.

phpa uses the readline module for command input and tab completion. This module is required to use phpa.

Using phpa

After starting phpa, you will see the >>> prompt. You can enter nearly any PHP code here.

phpa tries to be smart about the code you give it. If the code looks like an immediate (i.e. doesn't contain assignments, blocks, control structures, etc.) and doesn't produce any output, then the result of the code is printed. This makes it easy to test various things without unnecessary typing.

A newline is automatically added to the end of any output that doesn't already end with one.

phpa supports tab completion for functions and constants. This works like filename completion in the bash shell. Start typing the name of a function or constant then press the tab key to complete the name or see a list of possible matches.

Example usage

Simple immediates:

>>> 1 + 1
2
>>> "hello" . chr(10) . "bye"
'hello\nbye'

Variables:

>>> $x = pow(2, 10)
>>> echo $x
1024
>>> $x * 4
4096

Arrays:

>>> $a = array("a" => "apple", "z" => "zebra")
>>> $a
Array
(
    [a] => apple
    [z] => zebra
)
>>> $a["z"]
'zebra'

Compound statements:

>>> $m = 5; $n = 10; echo $m * $n
50

Builtin functions:

>>> php_sapi_name()
'cli'
>>> zend_version()
'1.3.0'
>>> filesize("/etc/passwd")
2472

User defined functions:

>>> function cube($n) { return pow($n, 3); }
>>> cube(3)
27
>>> cube(5)
125

Complex builtin functions:

>>> mysql_get_client_info()
'4.0.20'
>>> $mysql = mysql_connect("localhost", "test", "test")
>>> mysql_get_server_info($mysql)
'4.0.20'
>>> mysql_get_host_info($mysql)
'Localhost via UNIX socket'

Bugs

Due to some inherent limitations in PHP, it's not possible to do everything that I would like, such as trapping fatal errors. But I might have missed other things that could work better. If you run into something that doesn't work the way you think it should, please let me know!

Alternatives

PHP Interactive is a web based interactive shell for PHP.

PHP Shell is similar to phpa, but takes an object-oriented approach and has more features, including fatal error handing.


David Phillips david.acz.org Valid HTML 4.01!