Execute php on commandline with the interactive mode

Sometime you quickly want to check some PHP code you’ve read about,  just want to try something out (f.e a snippet from https://www.ask-sheldon.com ;-)) or even want check your PHP configuration. In these cases PHP has a very nice feature since version 5.1.0 . The interactive mode. You can start it by calling PHP with the -a parameter on the commandline. The following example shows how I checked if shell_exec was enabled correctly:

$> php -a
Interactive mode enabled

<?php
$shellOutput = shell_exec('ls -lah');
echo "<pre>$shellOutput</pre>";

You have to finish your entry with ctrl+d to execute the script. Afterwards the script output – in this case a directory listing of the current folder – is written on the screen.

Additional information about the interactive mode

For more information about PHP’s interactive mode have a loo at the official documentation here.

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.