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.
Example:
* * * * * /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!";
exit;
}
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.
Example:
root@server:/var/htmlu/php -v
Example response:
PHP 7.2.16-1+ubuntu18.04.1+deb.sury.org+1 (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 v7.2.16-1+ubuntu18.04.1+deb.sury.org+1, 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.
Example:
/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.
Example:
/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!
Thanks for helping in this issue, it solved my query very well. Subscribed your blog.