PHP CLI – how to run PHP programs from command line

With PHP CLI you can use PHP programming language to make programs and scripts which you can launch from command line. There is a separate article with instructions for PHP CLI and PHP GTK installation. If you have not installed them, read that article first.

Assuming that you have installed PHP CLI. You can test how it works. Create a text file test.php and write following lines in there

#!/usr/bin/php -q
<?php echo "Using PHP from command line"; ?>

First line isn’t necessary, but if you put it there, and make the file executable, then you can launch that program directly using its name. But you can always launch php programs with


php -q test.php

Those programs you created for Apache-PHP combination do not work from command line. They are meant to output HTML on web browser. But otherwise you have all the programming arsenal that PHP has to offer. This means that you can use all your skills in PHP programming to avoid using bash shell scripting. And certain things are actually easier to do with PHP CLI due to PHP extensions and modules.

With PHP you can easily create a program that make backups of some files, transfers them to other machine, and then sends an report of this activity through email. Then you just put that program as a cron job.

Leave a comment

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 105 other subscribers