Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 520
Javascript Coder Avatar asked Jul 24 '26 14:07

Javascript Coder


1 Answers

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]

like image 119
Lorenzo Marcon Avatar answered Jul 26 '26 05:07

Lorenzo Marcon