naivedeveloper
naivedeveloper

Reputation: 2958

Executing PHP Directly From the Command Line

I've browsed quite a bit, but I'm unable to come up with a solution. I'm attempting to execute a mixed PHP/HTML stream directly from the command line, not from a file. I'm aware of the command line switches, -f (to execute from a physical file), and -r (to execute PHP only). Basically, I'm looking for the following functionality:

php -x "<html><head></head><body><?php echo 'Hello World'; ?></body></html>"

Note that -x is not a valid switch, and the argument is an arbitrary stream of characters with mixed HTML and PHP. The output of this program, would ideally be:

<html><head></head><body>Hello World</body></html>

Upvotes: 0

Views: 435

Answers (2)

Sjoerd
Sjoerd

Reputation: 75578

Pass the code to standard in, without any command line switches:

echo '<html><?php echo "hello world"; ?>' | php

Upvotes: 1

Paul
Paul

Reputation: 141829

You can use this:

echo "<html><head></head><body><?php echo 'Hello World'; ?></body></html>" | php

Note that the first echo is a bash echo, not a php echo.

Upvotes: 6

Related Questions