PHP in a shell script

Just do a simple echo of the script, and pipe to php or php -q

echo '<?php print("hello world\n"); ?>' | php

A more useful example is using this in a bash script with here documents, that way you don't have to escape characters.

#!/bin/bash

( cat <<eof
<?php
print("hello\n");
?>
eof
) | php

If you want to add variables such as $str = "hello world"; you have to escape the dollar sign. Here is the script.

#!/bin/bash

( cat <<eof
<?php
$str = "hello world\n";
print($str);
?>
eof
) | php

Page Comments (Click to edit)

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Powered by pmwiki-2.2.0-beta65