MikeiLL
MikeiLL

Reputation: 6570

Can PHP CLI return immediate results in the terminal window?

I'm trying to understand how the php cli is supposed to work. Do you have to run everything as a script like:

Filename: helloworld.php

<?php
print "Hello World!";
?>

followed by:

chmod +x helloworld.php
php helloworld.php

Or is there a way to have it interact the way python does?

I tried using the -a option but am still not getting any results.

Upvotes: 2

Views: 620

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146603

I can't speak Python but I suppose you're looking for an interactive console as, for example, the one found in many JavaScript platforms such as Node:

C:\>node
> var x=2, y=3;
undefined
> console.log(x*y);
6
undefined
>

As far as I know, PHP does not have such feature. The PHP interpreter needs to parse the complete script before it's able to execute it.

There is an interactive mode but it expects a single code block—it isn't a console:

C:\>php -a
Interactive mode enabled

C:\>php -a
Interactive mode enabled

<?php
$x=2;
$y=3;
^Z

C:\>php -a
Interactive mode enabled

<?php
echo $x*$y;
^Z

Notice: Undefined variable: y in - on line 2

Call Stack:
    7.7505     355096   1. {main}() -:0


Notice: Undefined variable: x in - on line 2

Call Stack:
    7.7505     355096   1. {main}() -:0

0
C:\>

I'd like to be able to input true == false; and see the result.

The closest you can get is -r:

C:\>php -r "var_dump(true == false);"
bool(false)

Don't forget shell escaping rules.

Upvotes: 1

Related Questions