PHP: What Can You Do with It on Your System i?

Web Languages
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

PHP takes you where RPG can't: into a world of heterogeneous operating systems running the same language.


PHP is a highly productive programming language for developing Web applications. As I mentioned in an article in Showcase magazine ("PHP: A Party Waiting for a Few Good Developers"), PHP has been around since 1995 on other platforms and has gone through as many transformations as RPG has during that time. In this article, I'll discuss what PHP can do for you and why you should consider using it.


PHP is open-source software. Although a private company, Zend, provided the version of PHP for IBM that runs on the System i, the base Zend parser is still open source, with developers around the world continuing to contribute to its code base. (Source:

PHP on System i

PHP can (and does) run on many operating systems, including Linux, UNIX, and BSD-based OS's, besides Windows and System i. At this point, System i is still comparatively a niche market for PHP, as it was only recently made available in an easily installable form with special extensions to support the native i5/OS database DB2.


On the System i, PHP runs as an Apache extension called Zend Core. Apache is the most widely used non-proprietary Web server. A customized version runs on the System i. It is also open-source code and runs on all the other platforms I mentioned earlier, with a common code base for those. You can run PHP as a CGI or with the  ISAPI module in Microsoft's Web server, IIS, but there are weaknesses with this approach as described on this page.

When Zend ported PHP to the System i, they took advantage of the PASE environment, which lets you run a UNIX partition directly in i5/OS. The Apache server actually acts as a proxy server when it encounters an HTTP request that is intended to run a PHP script and then farms it off to a PASE environment job, all transparently to both the end-user and the system administrator. This was a brilliant idea on Zend's part because it meant they didn't have to rewrite the entire code base. Instead, PHP essentially runs like it would on any UNIX box. In order to take advantage of i5/OS-specific features, though, Zend needed to write functions that cross the boundaries between the UNIX world and the native i5/OS world. These include functions for accessing the native DB2 database, calling i5/OS programs, and working with i5/OS objects such as data areas, data queues, user spaces, and spool files.


So, what can you do with PHP on your System i? 

•·        Create new Web applications that use the native DB2 database

•·        Create new Web applications that use a MySQL database installed on the System i

•·        Interface to legacy applications by directly calling ILE programs or modules


What can't you do?

•·        Replace RPG totally with PHP. For example, you'll still need RPG for batch programs and for report programs that generate spool files (if you need standard text-only reports versus HTML reports).

•·        Replace CL totally with PHP. CL is highly specialized for running system commands. However, the i5 PHP API toolkit does include a function that lets you embed calls to CL commands directly in your PHP script. This feature allows you to run many, but not all, CL commands. You cannot run those commands that require the execution environment to be a compiled CL program, for example. 

•·        Web-face existing interactive RPG programs. You'll need some other tool, such as HATS or third-party vendor tools, if you want to re-face and run green-screen programs in a browser.

Creating Web Apps with PHP

So the primary role of PHP is to create "native" Web applications, those that use the HTTP protocol in a typical way, which generally involves having the browser directly communicate with the Apache Web server, which in turn draws on the resources of the underlying i5/OS operating system to perform data manipulation and computations.


When you use PHP in conjunction with the latest Web 2.0 technologies, you can create Web applications that are highly responsive to users and provide sophisticated interfaces and immediate, event-driven feedback. These technologies are now commonly available and mostly open source. They include JavaScript libraries, plug-ins, and frameworks, in addition to the standard client-side Web programming facilities such as HTML and CSS.


We are starting to see some powerful Web 2.0 interfaces in common use now on public, commercial sites. For example, Flickr, the online photo management site, has an application for organizing your photos into sets simply by dragging and dropping thumbnails of the photos on a work area. As you release the mouse to place a photo in the set, a back-end server process kicks in that updates your Flickr database to record the new location of the photo persistently. Another example is Facebook, the popular social networking site. There, you'll see messages or prompts for more information pop up in your browser when you confirm your response to a "friend request." Rather than reload the entire page, Facebook's server just sends a "page partial" and updates the back-end Facebook database.  


However, none of the Web 2.0 sites actually require that the back-end programming language be PHP. You could use any Web language to interact with the client-side technologies that make this cool stuff happen. So why would you choose PHP for your server-side language? You could choose RPG or any number of third-party tools that use RPG as their basis. However, PHP has some significant advantages over RPG for Web development.

Functions, Functions, and More Functions

PHP has thousands of functions to support Web application development. For example, there's a set of over 50 PDF writer functions that enable you to create sophisticated PDF documents programmatically with PHP. These functions are documented on the Web site. The list of function categories alone must contain over 200 items (I didn't count them all). Just about any Web programming need you can conceive of is covered by these functions. For example, in the calendar category there is a function called easter_date, which, you guessed it, returns what day Easter falls on in any given year. If you've ever written a calendar application and had to try to figure that one out, you'll recognize the depth of research that has gone into building all these PHP functions. In case you are interested, here's the formula to compute Easter.


You might find the LDAP set of functions more relevant to your application development needs. LDAP lets you have a single location (a "directory") for storing login information  for signing on to a network of heterogeneous systems, which lets you approximate the nirvana of single sign-on. Your users will still have to log in to any sites you secure with LDAP via the browser, even if they use the same directory to log in to Windows or their System i sessions, but at least there's a central place where all user profile information is stored.

PHP: An Open-Source Language for Web Development

PHP is relatively easy to write, and it was designed specifically for Web development. It's a loosely typed language, so it's quite forgiving in how you reference variables. It's also a procedural, imperative language, meaning it uses semantic ideas similar in concept to RPG. For example, RPG has subprocedures and PHP has functions. Of course, PHP is a free-format language, and it uses syntactical features similar to most common languages such as C, C++, C#, Java, JavaScript, Visual Basic, free-format RPG, etc. You can also write PHP as object-oriented code, so if you are used to writing in Java, the transition to PHP should be fairly easy. PHP lets you interleave HTML and server-side code, like many of the Web development languages do. This can be a challenging concept for green-screen RPG programmers to get their heads around initially, as they are used to coding the UI (display file DDS) independently of the logic (RPG code), while the most common style of programming PHP is to simply break into the logic at any point you need to and start writing HTML. PHP also supports including variables in any output HTML simply by including the variable name inside the content.


Because PHP is open source, you also benefit from a huge reserve of code that you can mine. Many PHP Web sites provide complete working PHP applications you can download and use for your own needs. Sometimes, you may use these simply as learning tools; other times, you may actually want to incorporate the code. Check the licensing terms of the code to make sure you are not violating copyright before you choose the latter option, although generally you'll find most PHP code is liberally licensed. One caveat, though: There's no guarantee the code you are looking at is well-written, let alone works correctly. You'll probably come across a lot of mediocre code, as well as nuggets of real value.


PHP is stored in ASCII text files. While RPG programmers are likely to use SEU or WDSc to edit source, you can write PHP in any PC-based text editor, such as Notepad or SlickEdit, or any PHP-specific editor, such as Zend Studio or Eclipse with the PHP plug-in. You can also use third-party tools such as BCD's WebSmart for PHP to generate PHP from templates and edit it. This tool is written specifically to take advantage of the System i platform. 


With PHP, you store the source files on the IFS and map the appropriate IFS directory to a URL in your Web server configuration so that users can run the scripts from their browsers. When the scripts run, they deliver the Web content (usually an HTML page) to the browser. It's important to realize that even though the PHP script source file is invoked directly during this process, the end user never sees this source, just the results of it's execution, which is the delivered page.

PHP is primarily an interpretive language. Critics of PHP often claim that this reduces its commercial appeal because of potential scalability issues. However, many large Web sites run PHP successfully, with considerable amounts of traffic. In addition, Zend has a product (Zend Platform) that mitigates this issue by providing load balancing, source-code caching, and on-the-fly code optimization.


People often wonder if PHP is really suitable for business applications. That may be because many early visible implementations of PHP were for apps such as bulletin boards, calendars, or public portals. These don't necessarily resonate with System i developers. However, PHP can be used for mission-critical, commercial applications too. A good example of a commercial application is SugarCRM, which is a Customer Relationship Management (CRM) product. It is suitable for large organizations and uses MySQL as the back-end database to store information about customers, calls, salespeople, etc. SugarCRM is open source (but not necessarily free) and has been ported to the System i. Of course, you are not limited to creating CRM applications with PHP. Because it can work directly with the native i5/OS DB2 database, you can build all the same types of applications you'd want to build as 5250 interactive, but with way more functionality. These include order entry, accounting, payroll, sales analysis, utility billing, online shopping carts, enterprise portals, hospital patient tracking, etc. Really, anything you can think of that's a typical business problem that needs solving you can write in PHP.


To get started with PHP, consider following this plan:

•·        Select a small, low-risk, well-defined development project.

•·        Take a couple of PHP classes.

•·        Learn HTML and CSS (in a later project, you'll want to learn JavaScript also).

•·        Read Zend's Zend Core User Guide for i5/OS. This explains the APIs that let you use System i-specific functionality in PHP.

•·        Install Zend Core on your System i (this is not complicated).

•·        Download some of the mountains of available source code.

•·        Try one of the PHP editors or development tools on a trial basis.

•·        Start writing code.

•·        Deliver and then watch the smiles on your users' faces!


One final advantage of PHP is that it is supported across multiple platforms. That means the gene pool for developers is much larger than the RPG gene pool. Most Computer Science students have at least some familiarity with PHP these days and are much more likely to want to learn PHP than RPG. This gives you the option down the road to easily share the strengths of other platforms such as Linux or UNIX without having to learn new technologies, yet while still leveraging the strengths of the System i. PHP succeeds where Java has failed: It's simple to learn, lightweight (does not require lots of system resources), and easy to install and run. PHP takes you where RPG can't: into a world of heterogeneous operating systems running the same language so you can leverage your programming knowledge and have skills that will be applicable for many years.