How to run PHP in interactive mode?

6

How do I run php from the command line, without having to use 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 is there any way to do this interactively (as in python, for example)?

Something similar to this example:

> php 

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

Is there this in PHP?

    
asked by anonymous 30.07.2015 / 17:21

3 answers

3

You can use this:

C:\>php -a 
Interactive mode enabled 

<?php 
echo "Hello, world!"; 
?> 
^Z 
Hello, world!

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

See more at: link

    
30.07.2015 / 17:32
2

The iterative mode of PHP does not work very well on all systems (Windows).

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

I install it via composer, as a global dependency:

composer global require psy/psysh:@stable
    
30.07.2015 / 18:51
1

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

php -a or php.exe -a (if using Windows)

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

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

link

    
03.08.2015 / 16:58