PHP CLI Tips and Tricks

Read this post if you want some excellent PHP CLI tips and tricks. Why will this post help you? Well, identifying PHP CLI script problems can be difficult because the errors are not immediately visible. Therefore, you will save yourself lots of time and effort by using these PHP CLI tips and tricks!

Tip 1 – PHP scripts via CRON

Specify the full path to the PHP CLI executable and PHP script. CRON does not know the full path to the PHP executable.

Wrong CRON config:

* * * * *       php /var/html/myPhpCode.php

Note: Unless you set the path to the PHP executable in the environment path config for the profile. Read more here.

Right CRON config:

* * * * *       /usr/bin/php /var/html/myPhpCode.php

Tip 2 – Pipe PHP CRON script output to a log

Save yourself a ton of effort and pipe the PHP CRON output to a log file. It will make it easy to see any errors and allow you to post messages for debugging purposes.


* * * * *       /usr/bin/php /var/html/myPhpCode.php >> /var/html/my.log  2>&1

The CRON entry (as mentioned above) will output all standard and error output in append mode to ‘my.log’. You can also change the ‘>>’ to ‘>’ to overwrite the ‘my.log’ file instead of appending content.

Tip 3 – Pass arguments to your PHP CLI script

CLI arguments can enhance your PHP script and allow interactivity via the command line.

Provide an argument to your PHP script like this:

/usr/bin/php /var/html/myPhpCode.php theArg

Example code:

 if (isset($argv[1])) {
     $myArg= $argv[1];
 } else {
     $myArg= "";
     echo "no arguments have been set!";

Tip 4 – Check the version of PHP

PHP syntax and functions can be version-specific. You can check the version of PHP using the -v argument.


root@server:/var/htmlu/php -v

Example response:

PHP (cli) (built: Mar  7 2019 20:23:29) ( NTS )
 Copyright (c) 1997-2018 The PHP Group
 Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
     with Zend OPcache, Copyright (c) 1999-2018, by Zend Technologies

Tip 5 – Enable “display errors” via the PHP CLI

Enable and display any PHP errors by adding the -d argument.


/usr/bin/php -d display_errors=1 /var/html/myPhpCode.php

Tip 6 – Test your PHP CLI Script without the php.ini config

You can disable the php.ini config when you run your PHP CLI script.


/usr/bin/php -n /var/html/myPhpCode.php

Tip 7 – Check your syntax via the PHP CLI

Use the PHP CLI to check your PHP language syntax by using:

/usr/bin/php -l /var/html/myPhpCode.php

Example response:

No syntax errors detected in /var/html/myPhpCode.php

Tip 8 – The PHP CLI config file

PHP and PHP CLI uses different php.ini files. Locate the PHP CLI file by using:

php -i | grep php.ini

Example response:

Configuration File (php.ini) Path => /etc/php/7.2/cli
Loaded Configuration File => /etc/php/7.2/cli/php.ini

Tip 9 – View CRON logs

You can use the following to filter the Syslog for CRON output:

sudo grep – color -i cron /var/log/syslog

Example response:

Apr 11 03:15:01 ap-2a-adonis CRON[26625]: (root) CMD (/var/html/myPhpCode.php >> /var/html/myPhpCode.log 2>&1)

Tip 10 – View Apache Logs

The Apache error logs will prove to be your friend when debugging PHP CLI issues.

The location of the apache error logs depends on your Linux configuration:

  • RHEL / Red Hat / CentOS / Fedora โ€“ /var/log/httpd/error_log
  • Debian / Ubuntu Linux Apache โ€“ /var/log/apache2/error.log
  • FreeBSD Apache โ€“ /var/log/httpd-error.log

Wrapping up

Hopefully this PHP CLI Tips and Tricks post has helped you. Let Anto Online know if you have either great tips!

You may also be interested in

About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee

About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

One Comment on “PHP CLI Tips and Tricks”

Leave a Reply

Your email address will not be published.