If you work as a web developer with PHP and all of a sudden you need a script to do something specific for you on a system (say as a cron job or such), there’s no real need to switch to another scripting language like Perl or Python if good old PHP will do the trick!
True, we could always call the PHP script via a WGET call to the localhost server and let our webserver (say Apache) handle the process, but in this case I specifically want to run the PHP file like I would any other bash script.
So how do we do it then?
Well PHP ships with a handy command line tool which has a whole lot of functionality built into it (like checking a file’s syntax with the -l switch for example) and one of its abilities is to interpret and execute a PHP script.
To do this, you feed it the control parameter of -f together with the script path and Bob’s your uncle. If you want to expand upon this and force the PHP script to be executed within the contraints supplied by a php.ini file, you simply specify the .ini file to use with a control switch of –php-ini.
So where are these files stored then?
Well the default PHP installation folder generally is the /etc/php5 folder and more often than not, you’ll locate your php.ini file sitting under /etc/php5/apache2/php.ini. The PHP command is usually added to your environment on install, meaning that executing a PHP script from the command line is as simple as running:
php –php.ini /etc/php5/apache2/php.ini -f synchronize_script.php
…from anywhere on your system. Useful, isn’t it? :)