I don't know if it needs to be stated, but adding a few things to the above (and other) scripts will help.
Adding a -q option to the first line will keep the HTTP 'content-type' tags from printing out.
Also, you still have to put everything within <?php and ?> tags for everything to be parsed and executed.
#!/usr/local/bin/php -q
<?php
echo "Hello, I am a php within a shell\n";
?>
PHP is a great, easy tool for writing server management scripts...especially for managing mysqld.