• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Speed Up Command-Line PHP

    January 21, 2019 Alan Seiden

    While PHP runs quickly via the Apache web server, command-line PHP scripts (run from PASE directly or via CL or RPG) have a lag on start-up. In this article, I show how to speed up PHP when called from the command line (known as PHP-CLI).

    Why does PHP-CLI (command line PHP) have a slow start-up? While several reasons are often given, I’ve found the culprit to be the loading of PHP extensions that are enabled by php.ini and other configuration files of Zend Server. Examples of extensions are ibm_db2, simplexml, and Zend’s proprietary components.

    Within a normal web server environment, loading extensions doesn’t slow down PHP, because FastCGI preloads all configured extensions when it preloads PHP. On the other hand, when you run PHP from a command line (or from CL or RPG), PHP can’t be pre-loaded; it’s loaded at runtime. In that case, several seconds may be required to load extensions.

    You can use the –no-php-ini and –define options to speed up PHP-CLI. Here’s a quick reference from http://php.net/manual/en/features.commandline.options.php:

    –no-php-ini Ignore php.ini completely.
    –define Set a custom value for any of the configuration directives allowed in php.ini.

    Note that all examples below assume PHP 7, which is installed in /usr/local/zendphp7, but you could substitute any valid recent PHP version or location.

    Let’s consider a simple script:

    <?php
    
    // simple.php code
    
    echo "PHP script running.\n";
    
    ?>
    

    Simple.php outputs a message to show that it is running. Because we wish to measure the overhead of running a script, not the time required by the script itself, a simple script is best. Save simple.php somewhere on your IFS. Ensure that your user profile has *R permissions and all directories above it have at least *X. For our example, let’s save it in /www/zendphp7/htdocs, although it could go anywhere.

    Run simple.php

    First, go into a PASE terminal. (An SSH shell with Bash would work better, but I realize that you may not have Bash installed, so I’ll demonstrate using traditional QP2TERM.)

    CALL PGM(QP2term)
    

    Now let’s go to the directory of the PHP 7 binary:

    cd /usr/local/zendphp7/bin
    

    Run the speed test using the default php.ini:

    php /www/zendphp7/htdocs/simple.php
    

    If you used a stopwatch, you’d notice that the test needed up to a couple of seconds to run, even though the script did virtually nothing.

    Now add –no-php-ini, which prevents PHP from reading any .ini file, and thus not loading any extensions.

    php --no-php-ini /www/zendphp7/htdocs/simple.php
    

    Using the –no-php-ini option, you’ll observe that the script returned almost immediately.

    Our choices aren’t “all or nothing.” We can add configuration values on the command line.

    Run our test script again, this time adding –define to load the ibm_db2 extension, a commonly used extension for IBM i.

    php --no-php-ini --define extension=/usr/local/zendphp7/lib/php_extensions/ibm_db2.so /www/zendphp7/htdocs/simple.php
    

    The script still ran quickly with a single extension loaded.

    Speed at your command

    I have demonstrated that PHP-CLI runs substantially faster when we cut back on extensions, using PHP’s –no-php-ini and –define options. Try this technique and enjoy the improvement.

    An IBM Champion and founder of Seiden Group, Alan Seiden leads a team that mentors clients in building APIs and web/mobile applications using open source, PHP, Python, Node.js, and IBM i business logic. Alan’s passion for the IBM i community inspires him to host the bi-annual CIO Summit and offer a free monthly tips newsletter.

    RELATED STORY

    Custom PHP CLI php.ini configuration on #IBMi

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: CL, FHG, Four Hundred Guru, PHP, PHP-CLI, RPG

    Sponsored by
    Raz-Lee Security

    Start your Road to Zero Trust!

    Firewall Network security, controlling Exit Points, Open DB’s and SSH. Rule Wizards and graphical BI.

    Request Demo

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: The Challenge To Selfhood Infor Raises $1.5 Billion, Teases An IPO

    2 thoughts on “Guru: Speed Up Command-Line PHP”

    • Martin Hieden says:
      January 24, 2019 at 2:22 am

      Hello Alan,
      Maybe it’s also worth to mention that you can use the –php-ini option to start PHP from command line using a separate ini-file. This comes in especially handy when you have multiple CLI calls, which all use the same configuration.
      Best Martin

      Reply
      • alanseiden says:
        January 31, 2019 at 10:54 pm

        Thanks, Martin! You make an excellent point about –php-ini. I plan to write a second article about PHP-CLI, demonstrating –php-ini and how to use multiple –define options.
        Alan

        Reply

    Leave a Reply Cancel reply

TFH Volume: 29 Issue: 4

This Issue Sponsored By

  • Maxava
  • ProData Computer Services
  • WorksRight Software
  • Dawn May Consulting
  • Manta Technologies

Table of Contents

  • The IBM i Base Did Indeed Move On Up
  • Infor Raises $1.5 Billion, Teases An IPO
  • Guru: Speed Up Command-Line PHP
  • As I See It: The Challenge To Selfhood
  • IBM i PTF Guide, Volume 21, Number 2

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23
  • SEU’s Fate, An IBM i V8, And The Odds Of A Power13
  • Tandberg Bankruptcy Leaves A Hole In IBM Power Storage
  • RPG Code Generation And The Agentic Future Of IBM i
  • A Bunch Of IBM i-Power Systems Things To Be Aware Of
  • IBM i PTF Guide, Volume 27, Numbers 21 And 22

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle