• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    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

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

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