The built-in web server is a great way to quickly see your PHP code in action. Another handy tool for exploration and testing is the PHP REPL. Run php -a and you get a php > prompt at which you can type in some PHP code and immediately see the results, as shown in Example 19-3.
Example 19-3. Using the PHP REPL
% php -a
php > print strlen(“mushrooms”);
php > $releases = simplexml_load_file(“https://secure.php.net/releases/feed.php”);
php > print $releases->entry->title;
PHP 7.0.5 released!
In Example 19-3, the initial % is a Unix shell prompt and php -a is what you type to run the PHP REPL. The REPL then prints Interactive shell and a php > prompt.
It executes what you type when you press the Return key and prints any results. Typing print strlen(“mushrooms”); (and then Return) tells the REPL to run strlen(“mushrooms”) and pass the results to print, so it prints 9. Don’t forget the trailing ;—PHP code typed into the REPL follows the same syntax rules as PHP code you write in a regular program.
If you just typed strlen(“mushrooms”); the code would execute without any errors, but you wouldn’t see any output before the next php > prompt. The PHP REPL only displays something if the PHP code you enter creates output.
The REPL remembers variables between commands. Entering $releases = simplexml_load_file(“https://secure.php.net/releases/feed.php“); uses the simplexml_load_file() function to retrieve the XML from the provided URL and store the results, as a SimpleXML object, in $releases.‘ SimpleXML provides a hierarchy of objects corresponding to the structure of the returned XML, so the value of the title element under the first entry element under the top-level XML element is $releases->entry->title. When the code in Example 19-3 was run, the first element in the releases feed was PHP 7.0.5.
There are other REPLs aside from the built-in one. A nifty example is PsySH. You can install it with Composer: php composer.phar global require psy/psysh.
That global before require tells Composer to install PsySH not in any package- specific directory but in a systemwide Composer directory. On OS X and Linux, this is the .composer directory under your home directory. On Windows, it’s AppData \Roaming\Composer under your home directory. For example, if you log in to your computer with username squidsy, then the Composer directory is /Users/ squidsy/.composer on OS X, /home/squidsy/.composer on Linux, and C:\Users\squidsy \AppData\Roaming\Composer on Windows.
The actual psysh program is put in a vendor/bin directory under the Composer directory. So, to run it from the command line, you either need to type out the full path (e.g., /Users/squidsy/.composer/vendor/bin/psysh) or add that vendor/bin directory to your system’s $PATH, the default set of directories it looks in for program names that you type.
Once you run psysh, you get a prompt at which you can type in some PHP code. Unlike the built-in REPL, it prints the value a statement evaluates to (even if you don’t include a print command) and uses different colors of text for different kinds of variables.
Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.