Running PHP on the Command Line

I’ve been exploring PHP recently and wanted to test some simple code in the PHP interactive shell. I was puzzled to find that I couldn’t seem to make anything work in the shell – I could enter commands, but didn’t see any output from them.

You can run php interactively by entering “php -a” in a command window. Here’s what I tried to do:

$ php -a
Interactive shell
 
php > $map=["answer" => 42]
php > $map["answer"]
php > array_key_exists("answer", $map)
php > echo $map["answer"]
php > print $map["answer"]

I created an associative array with one key (“answer”) in it then tried to print the value back. No response…no value was displayed. I tried calling the “array_key_exists()” function, again, nothing gets returned. No errors, no indication that anything is wrong.

It turns out that the PHP interactive shell requires a semicolon to terminate each command before it gets processed. If you press carriage return without a terminating semicolon, the shell is in “line continuation” mode, permitting a block of code (like a function) to be entered before it gets executed. The other trick with interactive PHP is that it doesn’t automatically display the value of an expression when it’s executed, even with a terminating semicolon. You have to explicitly use “echo” or “print” (or some other similar command) to get the value to appear in the output.

Knowing this, I could get the results that I wanted:

$ php -a
Interactive shell
 
php > $map=["answer" => 42];
php > print $map["answer"];
42
php > echo $map["answer"];
42
php > echo array_key_exists("answer", $map);
1

This is a little counterintuitive if you’ve used interactive shells for other languages like Python or Javascript – they tend to be more lenient about code input syntax. But then the rules are different in each of these languages: Python allows terminating semicolons on lines of code (but stylistically discourages their use – ‘pylint’ or ‘pycodestyle’ will warn about them). Semicolons are not strictly required by the syntax of Javascript, though it’s most often advisable to use them due to the possibility of concatenation problems – adjacent statements without a separating semicolon can sometimes interact in unexpected ways. PHP is much more restrictive about requiring semicolons at the end of statements (but the interactive shell doesn’t warn about their absence).

Here’s the same as above exercise in Python:

$ python
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> map={"answer": 42}
>>> map["answer"]
42
>>> "answer" in map
True

Here’s a version in Javascript (tested in node as well as Chrome developer’s console):

$ node
> map={"answer": 42}
{ answer: 42 }
> map["answer"]
42
> "answer" in map
true
> map.hasOwnProperty("answer")
true

If you enter an expression in interactive PHP and hit carriage return before entering a semicolon, you can fix it by entering a semicolon on the next line, completing the expression and allowing it to execute. If you enter more than one expression without a semicolon, trying to fix it with a semicolon will most likely result in an error message like this:

php > $map=["answer" => 42]
php > $map["answer"]
php > ;
 
Parse error: parse error in php shell code on line 2

But the interactive shell is now ready to accept a new expression at this point, so just re-enter the expressions, ending each with a semicolon. The PHP shell does provide command history recall with Control-P, so you can step back and enter selective expressions again, terminating each with a semicolon this time. PHP interactive shell also provides function completion with TAB.

TL; DR

Interactive PHP (‘php -a’) works a little differently from interactive shells in other languages:

  • Each complete expression must be terminated in a semicolon
  • ‘echo’ or ‘print’ or something similar has to be used to display the value of an expression or variable entered on the command line

If you enter an expression in interactive PHP and hit carriage return before entering a semicolon, you can fix it by entering a semicolon on the next line, completing the expression and allowing it to execute.

The interactive PHP shell offers other features, like command history recall with Control-P and completion with TAB.

References

The official documentation on using the PHP interactive shell:
PHP: Interactive shell – Manual

Details about how to make sure the interactive shell runs correctly (beyond terminating lines with semicolon):
How to execute PHP code from the command line? – Stack Overflow

Discussion of auto insertion of semicolons in Javascript, “statement compression”, and how semicolons are statement terminators, not statement separators in the language:
Do you recommend using semicolons after every statement in JavaScript? – Stack Overflow

Versions

$ php -version
PHP 5.5.38 (cli) (built: Aug 21 2016 21:48:49)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

$ python -V
Python 2.7.10

$ node -v
v5.0.0

Google Chrome browser Version 62.0.3202.94

Add a Comment