Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline PHP (command line)

I would like to make something like tryruby.org. I take a line from the user (e.g., echo __FILE__) and I want to execute it in PHP and return the output back to the client.

I tried to do exec('php -r ' . $command, $output), but $output always contains the PHP help section.

How can I implement this feature?

like image 913
balkon_smoke Avatar asked Apr 05 '12 08:04

balkon_smoke


2 Answers

http://tryruby.org seems have an interactive Ruby shell. That seems to be a good starting point.

Here are two projects that provide such a shell for PHP: php_repl and phpsh.

The other part is the web interface for the interactive shell. For that part, I suggest you have a look at repl.it, which provides this service for many languages (but sadly not PHP). Here's a link to it's source code.

With this combination, you should be able to complete cour project.

like image 153
Carsten Avatar answered Sep 21 '22 21:09

Carsten


To make php -r you have to have to put the code you want to execute between ' .. your code .. '

Example:

php -r ' $var = 34; print_r($var); '
like image 28
Andreas Helgegren Avatar answered Sep 23 '22 21:09

Andreas Helgegren