PHP for the RPG Programmer by Scott Orr

A little background:

Way back in 2007, right after the housing and financial market crash, I found myself in a stagnant, boring position as a maintenance programmer. After developing custom solutions for the previous 20 years I felt I either had to learn something new or exit the IT field.

I first looked at Java and I have to admit that it gave me a headache. Coming from the COBOL (in the 80’s) and RPG world, it just did not make sense to me. Then I came across a company called ZEND and how they recently developed a sub-system to interpret PHP code that runs on the iSeries. I bought a book, started reading and I was blown away. Finally a language that was very much RPG-like, running on the platform I love, that would enable me to design systems an RPG programmer could only dream about.

That pretty much ended my RPG programming career. I looked at the types of reports my users requested most frequently and set out learning how to make them happen in PHP. Since PHP on the iSeries was fairly new and I knew no PHP programmers, the learning curve was very long. Technical things that are second nature now took days or even weeks for me to resolve but in a few months I began rolling out reports that my users loved and created a menu system that let them run reports without IT. This soon became the solution of choice among the 30 or so users and almost cost me my job a few times when management found out. They were not interested in making the users more productive and looked at my system as a burden. I didn’t care. I was fighting the good fight and felt completely revitalized as a software solutions provider. I soon moved on from that job and now offer a PHP reporting/updating system for companies running the Infinium HR/PY/PL system as well as custom programming.

The goal of this series is to help and encourage any iSeries programmer who wants to learn something new and exciting while making their skill set more valuable. In today’s world of a shrinking iSeries footprint around the world I would highly recommend it.

If you wear many hats (PM, developer, QA, trainer, etc…) you may not have time to start from scratch in the OO world. Being self-taught, I have remained an asynchronous programmer which let me hit the ground running. There may be significant advantages to OO programming if you have a large-scale system with hundreds of users but I have never run into any performance issues with my code. The same is true with the display of the screens. CSS would be the preferred method but I use straight HTML. Probably more coding but still effective.

Let’s get started with something simple yet powerful.

There are some things commonly used in RPG that are offered in PHP and I find them still very effective. Both languages are very array intensive and both offer a type of LDA. The example I show below will make use of both. Part of our packaged software ( deals with employees and a drop down of employees is used on many screens.

The first thing we’ll do is create an array of employees and store it in PHP’s version of the LDA in a thing called a ‘session’ variable. They work exactly the same way so this code will be saved in a PHP file named ‘loadallemps.php’ and included wherever the drop down is needed but it will only be executed once per session.




1. <?
2. $colist = $_SESSION[‘validcomp’];
3. $sql  = “select * from $datadb.prlms where prtedh=0 order by prcnm;
4. $stmt = db2_exec($conn, $sql);
5. while($row = db2_fetch_row($stmt)) {
6. $er   = trim(db2_result($stmt, ‘PRER’));
$en   = trim(db2_result($stmt, ‘PREN’));
$name = trim(db2_result($stmt, ‘PRCNM’));
$desc = $name$en ($er)”;
7. if (array_key_exists($er,$colist)) {
       $c [$en] = $desc;}
8. }
9. $_SESSION[‘allemps’] = $c;
10. ?>
  1. All PHP scripts begin with ‘<?’
  2. A previously saved array of all companies I have access to is loaded to a local variable called ‘$colist’
  3. The SQL statement to read the employee file. We use a variable for the library ($datadb) to facilitate switching between production and test. If the ‘where’ variable is not created at the top of the script the variable is NULL and will be ignored
  4. Executes the SQL statement
  5. The ‘while’ statement is equivalent to a DOWEQ in RPG and will loop through all the rows found in the SQL statement
  6. Retrieve company, employee and name removing leading and trailing spaces from each column. Create a variable ‘$DESC’ to hold the employee name, number and company.
  7. Check to see if company is found in the array of companies I’m authorized to and if yes, store the employee in array called ‘c’ using the employee number as the index and the description as the value
  8. When all rows are read, save the employee array in a ‘session’ variable names ‘allemps’
  9. This session variable will be available to any program run during the user’s session
  10. All PHP scripts end with ’ ?>’



Now, in our main program we’ll include the code:

if (!$_SESSION[‘allemps’]) {



This reads as “if the session variable ‘allemps’ is NOT found, execute the script we just created above”. The ‘include’ equates to the RPG ‘copy’ and the ‘!’ before the variable $_SESSION[‘allemps’]  means NOT).  We’ll include this IF statement in every program that requires the employee dropdown but once it’s executed in our session the IF statement will always be false.

Back in our main program we’ll include ‘getemp.php’ in the location we want the dropdown to appear.

Here is the code that gets executed:

1. $emps = $_SESSION[‘allemps’];
2. $keys = array_keys($emps);
3. print “<td bordercolor=’#0000FF’ align=’left’><font size=’4′ color=’#014284′>Employees: </td>”;
4. print “<select name=’droplist’ ONCHANGE=’GotoURL(this.form)’ size=’1′><option selected=’selected’ VALUE=’ ‘></OPTION>”;
5. for ($x=0; $x < count($keys); $x++) {

$k = $keys[$x];

print “<option value=$k>$emps[$k]</option>”;}


  1. Brings the session array into a variable b=name $emps
  2. Creates a new array ‘$keys’ using the array values found in ‘$emps’ which are the employee numbers
  3. Print a table cell with the title ‘Employees:’
  4. Call the dropdown variable ‘droplist’. The ‘ONCHANGE’ is javascript that returns the selected drop down value to the program
  5. Loop through the employee number array ‘$keys’ writing each employee description to the drop down while using the employee number as the returned value

Finally, in our main program where the form begins we’ll include:

print “<form method=post>”;

print “<basefont face=’arial’>”;





function GotoURL(dl) {



// –>



The PHP code must be ended (‘?>’) so the javascript can be included then the PHP is restarted (‘<?’) after the javascript. The javascript code will return the selected row to the main program ‘hr070b’.


The more PHP coding you do, the more ways you’ll find to create code like this that is reusable saving you time and the effort of testing.


If you have questions or comments I can be reached at