Page 1 of 1
Running PHP script in CLI
Posted: Thu Dec 29, 2005 11:34 pm
by anjanesh
Code: Select all
#!/usr/bin/php
<?php
echo 'hello';
?>
./test.php
Code: Select all
[username@myserver username]$ ./test.php
Content-type: text/html
X-Powered-By: PHP/4.3.11
hello[username@myserver username]$
hello
is getting displayed but is it showing other stuff like Content-type etc ? Any idea what Im doing wrong ?
Infact I want to run one php script that doesnt outout at all.
Posted: Fri Dec 30, 2005 12:48 am
by josh
http://www.linuxjournal.com/node/6627/print
you may want to use the -q switch, which causes the interpreter to omit any HTTP headers that are normally required during a Web transaction.
....
This explicit method, however, is not very practical if you want your users to access the scripts you write conveniently. A better solution consists of making the scripts executable, so they can be invoked explicitly, as if they were autonomous programs. To do this, first determine the exact location of your PHP executable:
marcot ~# which php
/usr/local/bin/php
The next step consists of creating a shebang-an initial command that instructs the shell interpreter to pipe the remainder of an executable file through a specific application (the PHP engine in our case). The shebang must be the first line of your script-there can't be any white spaces before it. It starts with the character # and the character !, followed by the name of the executable through which the remainder of the file must be piped. For example, if you're using the CLI version of PHP, your shebang may look like this:
#!/usr/local/bin/php
If you're using the CGI version of the PHP interpreter, you also can pass additional options to it in order to keep it quiet and prevent the usual HTTP headers from being printed out:
#!/usr/local/bin/php -q
The final step consists of making your script executable:
marcot ~# chmod a+x feeder.php
At this stage, you can run the script without explicitly invoking the PHP interpreter; the shell will take care of that for you.
As you may have noticed, I have not renamed the script to remove the .php extension. Even though the extension itself is not necessary when running scripts from the shell, its presence makes it easy for text editors such as vim to recognize it and highlight the source's syntax:
marcot ~# ./feeder.php