How to run PHP in interactive mode?

Asked

Viewed 638 times

6

How do I run php from the command line without using a script as an argument.

Example with script:

> php index.php

I already know how to execute a certain function or code like this:

> php -r "echo 12346;"

But there is some way to do this interactively (as in python, for example)?

Something similar to this example:

> php 

> echo 1;
> 1
> print_r(array());
> array(){}

There is this in PHP?

3 answers

3


  • In mine only it worked without the <?php ?>

  • To work with <?php ?>, first you activate the interactive mode, type the <?php echo "Hello, world!"; ?> press ENTER then CTR+Z

  • what’s cool about this interactive mode ? and kind of like an Xcode playground ?

2

Iterative PHP mode does not work very well on all systems (Windows).

In those cases I like to use the Psysh. In addition to running in iterative mode, it includes functions such as autocomplete, namespace support, documentation query, and more.

I install it via Composer, as global dependency:

composer global require psy/psysh:@stable

1

To run PHP in interactive mode, just use the option -a, in this way:

php -a or php.exe -a (use Windows)

(as long as the php executable is in your PATH)

This and all other forms of PHP execution are described here:

http://rberaldo.com.br/diferentes-formas-execucao-php/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.