3

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

1
  • 1
    Are you running the commands directly in the console? Then, the first one fails, but the second one is not executing PHP's echo but the one of the shell ... Commented Jul 12, 2013 at 10:14

3 Answers 3

5

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]

Sign up to request clarification or add additional context in comments.

Comments

2

You are entering PHP code into the Unix shell (e.g. 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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.