简体   繁体   中英

CLI php run php in command prompt

I have installed PHP CLI to execute php commands from console.

I have installed PHP CLI using this command -

sudo apt-get install php5-cli

When I run this

$vr=3; echo $vr;

Result :-

=3: command not found

If I run echo "test"; Result :- test

displays..

Can anyone tell why "command not found" displays..

The "echo "test" line is working because echo is a bash command.

You have to write your own php script, the run it by command line like this:

$ php myscript.php

In alternative, you can run php from your command line, then directly write or paste your script. Then press CTRL + D to run it. Remember the at the beginning and at the end.

As third option, you can write a php script, putting in the first line this code:

#!/usr/bin/php

Obviously the php executable path must match the one in your system. This way, you can chmod +x the script, then run it directly like this:

$ ./myscript.php

The fourth option is the interactive shell :

$ php -a
Interactive shell

php > echo 5+8;
13

[$ in front of commands means a command run by user]

You are entering PHP code into the Unix shell ( eg bash). The Unix shell does not understand PHP code, so you have to run php first.

To run your PHP code from the command line:

$ php -r '$vr=3; echo $vr, "\n";'
3

To run your PHP code from the PHP interactive shell (which may or may not be compiled into PHP):

$ php -a
Interactive shell

php > $vr=3; echo $vr, "\n";
3
php >

(Hit Ctrl + D or type exit to get out of the PHP shell.)

To run your PHP code from a file named prog.php (which contains <?php before the code):

$ php prog.php
3

It seems you want something like this: http://www.php.net/manual/en/features.commandline.interactive.php

This gives you an interactive mode, where you can type PHP code and have it executed directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM