PHP command in Bash does not execute code

57
March 14, 2019, at 10:30 PM

Ok, so I have a problem with this thing I found on a MacBook Air. It's called terminal and you can do crazy stuff on it. So anyways, when I enter the command "php" it gives me a multiline console but it doesn' do anything when I run a line of php! For instance, I type echo "Hello World" but it just returns it like a typewriter and nothing happens! Can someone please tell me what is going on, and is there a way to exit this?

Answer 1

Firstly check if you have php properly installed:

type in console:

php -v

you should see version of installed php for ex.:

PHP 7.3.3 ...

to run single line of code from console you do it this way:

php -r 'echo "\nHello World\n";'

where \n is new line character.

to enter interactive mode and run multiple lines of code:

php -a

and once you see:

Interactive mode enabled

php >

type:

echo "\nHello World\n";

and hit [Enter] key.

that's it.

to leave interactive mode type:

exit

note lack of ; at the end.

If you want to run from console a php code that you have in a file:

php -f <path-to-the-file>

but this is a default behavior of php so if you miss flag -f and just type:

php

it will do nothing, expecting you providing it a path to file after the php like in the example with flag -f:

php <path-to-file>

So if a programmer intention is to enter an interactive mode but he types only php without any flags, the php will not warn about missing path to the file so programmer may have the impression he's into php interactive mode as he wanted but this is not true.

to see all possible options in php cli, type:

php --help
Answer 2

I believe if you just type php into the terminal it will start a php server process on the local mac. If you want to run a script you have to cd in to to the working directory and type php filename.php.

Answer 3

If you want to use PHP code directly you must enter php -a without a flag you will just enter in php environment.

Answer 4

When running the php command directly from your command line, it behaves as it is reading a .php from the command line

You noticed that it just echo's back any thing you write toward it, this is the same as it is executing a .php file, its echoíng any html back to the browser

Example:

$ php
sss
<?PHP
echo 'Hello!';
?>
More data!

ctrl + d (to signal that the end of the filehas reached)

Output:

sss
Hello!More data!
READ ALSO
Empty Post Data

Empty Post Data

So I'm working on a PHP projectToday wanted to start working on it again

24
why i am getting SMTP connect failed error in PHPMailer?

why i am getting SMTP connect failed error in PHPMailer?

I really don't know what's wrong in my codeI am creating contact us form by which user can send email to me

27
Use MacPorts PHP-FPM instead of from build

Use MacPorts PHP-FPM instead of from build

I've used MacPorts to install apache2, php73 and php73-fpm on MacOS MojaveI've updated by

44
Reduce memory usage for big database calls php

Reduce memory usage for big database calls php

I am using this script to cross reference data so that I can retrieve the right data from the database which requires 3 checks of which 2 are run within a for loop

54