28
Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM i Education Jon Paris Jon.Paris @ Partner400.com www.Partner400.com SystemiDeveloper.com 1 Notes This presentation may contain small code examples that are furnished as simple examples to provide an illustration. These examples have not been thoroughly tested under all conditions. We therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. All code examples contained herein are provided to you "as is". THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED. The authors, Jon Paris and Susan Gantner, are co-founders of Partner400, a firm specializing in customized education and mentoring services for AS/400 and iSeries developers. After individual careers with IBM, including working at both the Rochester and Toronto laboratories, Jon and Susan are now devoted to educating developers on techniques and technologies to extend and modernize their applications and development environments. Jon and Susan author regular technical articles for the IBM publication, IBM Systems Magazine, i5 edition, and the companion electronic newsletter, iSeries Extra. You may view articles in current and past issues and/or subscribe to the free newsletter at: http://IBMsystemsMag/iseries/. Feel free to contact the authors at: contact @ partner400.com or visit the Partner400 web site at http://www.partner400.com 2

Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Embed Size (px)

Citation preview

Page 1: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Programming With PHP:

An Introduction for RPG Programmers

Your Partner in IBM i Education

Jon ParisJon.Paris @ Partner400.comwww.Partner400.comSystemiDeveloper.com

1

NotesThis presentation may contain small code examples that are furnished as simple examples to provide an illustration. These examples have not been thoroughly tested under all conditions. We therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.All code examples contained herein are provided to you "as is". THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED.

The authors, Jon Paris and Susan Gantner, are co-founders of Partner400, a firm specializing in customized education and mentoring services for AS/400 and iSeries developers. After individual careers with IBM, including working at both the Rochester and Toronto laboratories, Jon and Susan are now devoted to educating developers on techniques and technologies to extend and modernize their applications and development environments.

Jon and Susan author regular technical articles for the IBM publication, IBM Systems Magazine, i5 edition, and the companion electronic newsletter, iSeries Extra. You may view articles in current and past issues and/or subscribe to the free newsletter at: http://IBMsystemsMag/iseries/.

Feel free to contact the authors at: contact @ partner400.com or visit the Partner400 web site at http://www.partner400.com

2

Page 2: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Agenda

What is PHPAnd how it operates on IBM i

The Basics of the PHP languagePHP variablesControl StructuresArray Processing

Other Examples

3

Notes

While we can't even begin to cover the entire PHP language in a single session, we hope to give you a taste of what the language brings to the table. Just enough to allow you to see just why PHP has quickly become the language of choice for implementing web based solutions on IBM i. Hopefully it will encourage you to study it further.

Far easier to learn than Java - and with a huge array of free education and software available - PHP is a great fit for the RPG shop that needs to deploy applications to the web quickly.

Hopefully by the end of this session you will understand why!

To facilitate your further studies we highly recommend "The IBM i Programmer's Guide to PHP" by Jeff Olen and Kevin Schroeder - published by MC Press. A lot of good basic PHP information and excellent examples of the IBM i specific functions. Some of the IBM i examples in this handout are based on examples from the book. And yes - they are used with permission from the authors.

4

Page 3: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

A scripting language for developing Web applications• But can also be used stand-alone in command line mode

Originally PHP stood for "Personal Home Page"• Just like RPG, it has come a long way from its roots

PHP is now a recursive (or self-referential) acronym• i.e. the first "P" in PHP stands for … PHP• The formal definition is - PHP: Hypertext Preprocessor.

At heart a procedural language• But V4 added OO capabilities

! Easier for an RPGer to learn than a pure OO language like Java! Use as much or as little OO as you need

Features a huge array of built-in functions• Additional functions provided by PHP extensions

What is PHP ?

5

The Zend - IBM agreementMulti-year contract to deliver PHP products for IBM i• In Feb '09 the agreement was extended for three more years

! See www.ibm.com/systems/i/software/php for details• PHP now comes preloaded on all V5R4, V6.1, and V7.1 systems• MySQL now included in Zend Server packaging

! It supports the IBM supplied MySQL DB2 Storage Engine- It is a great way to share data with your native applications

The new Zend Server has now replaced Zend Core• Show significant performance boosts• And does away with the second Apache server - Hooray!!!

! See www.zend.com/en/products/server/zend-server-5-new-ibmi• Also supports batch processing for PHP scripts

Zend Studio• Eclipse based so familiar to WDSC/RDi users• Integrated IDE for building and debugging PHP applications

! Available free of charge to all i5/OS® customers

6

Page 4: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

PHP Operation on IBM iPHP itself runs in the PASE environment•Communicates with the world via a native Apache server

! Set up for you during the install! By default on port 10088! That way it does not interfere with your regular HTTP server

IBM i Native Environment

Apache HTTP

Server

PASE

Zend ServerDB2

Access

Native Functions

7

NotesWhen you install Zend Server a number of components are installed for you.1) An Apache web server which is configured by default to accept requests on port 89 i.e. http://www.MyWebSite.com:89. This default is chosen in order to avoid any conflict with any existing web server operating on the conventional port 80. 2) The Zend Server PHP engine which handles all PHP requests and interfaces with the IBM i operating system.3) (Optional) MySQL - since this is the database of choice for most Open-Source PHP applications you are probably going to need it. Not only that, but with the advent of the DB2 Storage engine, you will be able to process your MySQL data with your regular RPG programs and vice versa.

Not only are the components installed, but the necessary configuration files are set up so that the resulting environment should "just work". You can test the install by seeing if you can connect to the port 89 server (http://YourServerName:89), which should give you access to the demo programs and the Zend configuration console.

8

Page 5: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

A Simple PHP Script

<?php

$myName = "Jon Paris";

$location = "Mississauga, Ontario, Canada";

$experience = 40;

$languages = array("RPG IV", "PHP", "Java", "COBOL", "CL", "Basic");

echo "This script was written by $myName.<br>";

echo "He lives in $location and has been playing with computers for over $experience years.<br>";

echo "Here are a few of the languages he has used so far in his career:<br>";

foreach ($languages as $language) {

print "$language<br>"; }

echo "<br><br>Print_r output<br>";

print_r($languages);

echo "<br><br>Var_dump output<br>";

var_dump($languages);

?>

9

Browser Output From Script

10

Page 6: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Understanding the Script - Part 1

<?php

$myName = "Jon Paris";

$location = "Mississauga, Ontario, Canada";

$experience = 40;

...

?>

PHP scripts are embedded in "code islands" within an HTML page•There can be multiple code islands on the page•Code islands begin with <?PHP and end with ?>

• Other combinations can be used but this is the normVariables are defined simply by being used• There is no equivalent to the RPG D-spec

• Error or warning may occur if a variable is used prior to assigning a value to it

Variable names in PHP must start with a “$” sign• Just when we'd finally begun to persuade RPGers not to do that!

11

NotesA PHP script begins with the characters <?PHP - this identifies the beginning of what is called a PHP "code island". As you will see later, code islands can be imbedded into HTML pages and it is this marker that allows the system to differentiate between the PHP and HTML components of the file.

At the start of the script proper we simply assign a value to the variable $myName. As you can see, PHP has no equivalent to RPG's D-specs - variables are simply defined by being used.

Of course it is good practice to group your variables' definitions, but the language does not enforce it. In fact PHP will happily allow you to reference a variable before it has been defined, but a good editor such as Zend Studio will warn you about this. In fact it is possible to have PHP treat such events as an error or warning depending on your PHP error handling settings. Look up the function error_reporting() in the PHP manual if you want to know more. The default setting for most web servers is to ignore such errors as PHP has a well defined default behavior for such instances.

12

Page 7: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Understanding the Script - Part 2

...

echo "<p>This script was written by $myName.</p>";

echo "<p>He lives in $location and has been playing with computers for over $experience years.</p>";

echo "<p>Here are a few of the languages he has used so far in his career:</p>";

...

Scripts add content to the page through the use of print and echo•The two are interchangeable for all practical purposes

Variables within literal strings are replaced by their content•As long as the string is delimited by double quotes “like this”• If single quotes are used no substitution takes place

13

NotesThe echo function is being used here to add content to the web page. Strictly speaking echo is a language construct in PHP and so does not require the use of parentheses around its parameters, although you will often see them used. We could also have used the print function as the two are used interchangeably (see below)

Note that the literal string being output is enclosed within double-quotes. Because of this any variables found within it will be replaced by their content when output. So what will appear on the web page is "This script was written by Jon Paris.". Had we used single-quotes the output would have been "This script was written by $myName.".

To achieve similar blending of literals and variables in RPG takes a lot more work. In this case we would have had to code something like this:

'<p>This script was written by ' + $myName + '.</p>';

While this may not seem a big difference in a simple case like this, it makes it much harder to keep track of spacing, punctuation etc. and I'm sure you can see how much simpler the PHP approach makes things, particularly when building more complex strings.

print and echo - what's the difference? The only significant difference between the two is that echo can take multiple parameters whereas print only takes one. For all practical purposes the two are used pretty much interchangeably, the choice as to which to use being mostly a matter of style and personal preference. For those of you who just "have to know" the full scope of the differences, you can find most of the gory details here: ((http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40)).

14

Page 8: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

A Simple PHP Script - Part 3

...

$languages = array("RPG IV", "PHP", "Java", "COBOL", "CL", "Basic");...

foreach ($languages as $language) {

print "$language<br>"; }

$languages is being defined here as an array•The function array() is being used to supply the values•As close to an RPG compile-time array as you will find in PHP

The foreach construct is one of my favorites• It provides automatic iteration though an array• It is used frequently when processing database result sets

15

NotesNow we come to one of my favorite PHP constructs - foreach.

This provides a really simple way to iterate over an array and apply the same processing to each element in turn. In our example we are telling PHP to take each of the elements in the $languages array and assign each in turn to the variable $language.

The code block within the { ... } brackets is then executed using that variable. We will talk more about the use of brackets in conditional expressions on later charts. All we are going to do in this case is to output the content of the variable - this time using the print construct.

We will be spending a lot more time on arrays later in the session.

16

Page 9: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

A Simple PHP Script - Part 4

...

echo "<br><br>Print_r output<br>";

print_r($languages);

echo "<br><br>Var_dump output<br>";

var_dump($languages);

?>

Array ( [0] => RPG IV [1] => PHP [2] => Java [3] => COBOL ...

array(6) { [0]=> string(6) "RPG IV" [1]=> string(3) "PHP" ...

This part demonstrates two very useful debug functions•print_r() and var_dump()

They output the variable content and other relevant information• print_r provides basic information• var_dump is more detailed and includes the data type and size

The text in red below shows part of the actual output from the two functions

17

NotesBefore ending the discussion on this first script let’s take a look at two great PHP debugging tools. Because PHP is so different in many ways from RPG, particularly in areas such as arrays, I found these two invaluable in my early explorations. Both fulfill the same basic function (to dump the contents of a variable) but they differ in the amount of detail that they provide. In some ways they are similar to RPG's DUMP operation code, but provide a degree of granularity not possible with DUMP. The resulting output is also a lot easier to navigate.

The first is the print_r() function. As you can see we simply pass the name of the variable we want to know about ($languages) as a parameter to the function. As a result, PHP will extract the contents of the array and format it for display. In our example the result will be:

Array ( [0] => RPG IV [1] => PHP [2] => Java [3] => COBOL [4] => CL [5] => Basic )

This tells us that the variable we passed was an array, and that the first element (i.e. The one with index [0]) contained the value "RPG IV". The second contained "PHP" and so on. I find it convenient to think of the "=>" combination as meaning "points to" - in other words, [0] "points to" the value "RPG IV".

The second of the debugging functions we are using here is var_dump(). It provides more detail than print_r() in that it identifies the data type of each element in the array and the length of its content. This can be very useful in cases where the content contains spaces since by default the browser treats consecutive spaces as a single space when displaying data. The output from our example looks like this:

array(6) { [0]=> string(6) "RPG IV" [1]=> string(3) "PHP" [2]=> string(4) "Java" [3]=> string(5) "COBOL" [4]=> string(2) "CL" [5]=> string(5) "Basic" }

The (6) at the beginning tells us that there are 6 elements in the array. Similarly the length and data type of each element in the array is identified.

When you first try using some functions (like web services, or database retrieval) it is really helpful to output the content of each variable in turn as you develop your code. Of course a good debugger (like the one in Zend Studio) can also show you the same information - but sometimes it is just easier to incorporate it into the script.

18

Page 10: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

More on PHP Variables

Variable names are cAsE_sEnSiTiVe!• So are function and object names etc.• But (most?) PHP reserved words are not

! So for example IF, if, If and iF are all accepted - at least by Zend versions

The name can include underscore, letters, and numbers• e.g. $_a_PHP_name, $AnotherName, $And_yet_1_more• But cannot begin with a number

There are only seven types of variables• A variable's type is determined by the value placed in it

! And yes - that does mean that it can change type!! Don’t panic - this is a good thing - More on this in a few minutes

19

More on PHP VariablesPHP currently supports four basic data types • boolean

! An indicator by any other name ...! A value of zero is false and a value of 1 is true - just like RPG!

- Actually any zero value or empty variable is also considered to be false• integer

! The same as RPG IV's 10I - A four byte integer! Holds values in the range -2,147,483,648 to 2,147,483,647• float

! The same as RPG IV's 8F - A double floating-point number! The _only_ way PHP has of expressing decimals• string

! Effectively the same as a varying length character field in RPG IV

$myString = "1"; // variable is a string $myInteger = 1; // variable is an integer$myFloat = 125.5; // and this is a floating point

20

Page 11: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Other PHP Data Types

Compound types• array

! Arrays are very very powerful in PHP! Nothing like RPG arrays! They can for example contain variables of different types• object

! Object references are very similar to array references! Once you know how to handle arrays, Objects are easy to

understand Resources• You will use these a lot• They are "handles" to things created outside of PHP itself

! For example a database connection, an image file! Or more importantly a System i connection, program object, etc.

21

NotesNote that although PHP (like most multi-platform languages) does not have an equivalent to packed decimal. Indeed the only numeric type that can accommodate decimal places is floating point. However, it does support decimal math when needed. We won’t go into it in detail in this session but here’s a brief example to point you in the right direction should you need it in the future.

Support is in the form of the BC Math functions such as bcadd, bcsub, bcmul, etc. These operate on decimal numbers represented as strings. This is similar to the way Java (for example) handles this situation. Here's a simple example of addition:

<?php

$a = '1.234';$b = '5';

echo bcadd($a, $b); // will output 6echo bcadd($a, $b, 4); // will output 6.2340

?>

Notice that the optional third parameter is supplying the precision (decimal places) required in the final answer. The example above outputs the results to the browser screen, but they could equally have been stored in variables. While not as elegant as a simple "echo ($a + $b);" you will find that you need to use this functionality far less often than you may think at first glance.

22

Page 12: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

<?php echo "<h3>The ever changing variable</h3>"; $myVar = "1"; // variable is a string with the value 1 echo "<br>\$myVar is now <b>" .gettype($myVar) . "</b> value $myVar<br>"; $myVar = $myVar + 2; // now it is an integer with the value 3! echo "<br>\$myVar is now <b>" .gettype($myVar) . "</b> value $myVar<br>"; $myVar = $myVar / 2; // and now floating point with the value 1.5 echo "<br>\$myVar is now <b>" .gettype($myVar) . "</b> value $myVar<br>";?>

Variables in PHP are dynamically typedOne minute they can be a character fieldThe next they are an integer!

Much of the power of the language derives from this abilityBut is can cause "interesting" debugging challengesJust like the MOVE op-code in RPG!!

Take a look at the example belowThe type of $myVar changes based on the way in which it is used

Changing a Variable's Type

23

The ever changing variable

$myVar is now string value 1

$myVar is now integer value 3

$myVar is now double value 1.5

Output Result

24

Page 13: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

The equality test operator is == • At first most RPGers will fall into the trap of using a simple "="

Sadly the resulting code is usually syntactically valid• It just doesn’t do what you want as the example demonstrates

Another difference is that in PHP expressions must be in parens ( )• Although this practice is also highly recommended for RPG code

<?php$age = 60;$limit = 70;$fare = 100;echo "Right now your age is $age<br>";

// The following line will erroneously print the message!if ($age = 70) echo "At $age You are at the limit of $limit!<br>";

// reset the value of $age and test again$age = 60;// But this one with the correct form of the test will notif ($age == 70) echo "At $age You are at the limit of $limit!<br>";?>

Conditional Expressions in PHP

25

The basic control structures match very closely to RPGBut multi-line actions require the use of the { } brackets

Some conditional operations have no direct equivalent in RPGe.g. The Ternary Operator - Although it will look familiar to Excel users

$age = 70;$fare = 100;

if ($age >= 60) $fare = $fare * .9;

echo "Age: $age means a fare of \$$fare";

// Simple test/action pairs can be done in one line$StockLevel = $Stock > 100 ? "OK" : "Low";

// The PHP code above is equivalent to the following

If ($Stock > 100) { $StockLevel = "OK"; }Else { $StockLevel = "Low"; }

Note the use of the { ... } pairs

A single operation so no need for { ... }

Expression is in parentheses

Control Structures in PHP - IF

26

Page 14: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Switch / Case / Default is similar to RPG's Select / When / OtherBut can only test the content of a single variable

This will be familiar to you if you know COBOL or C or ...Beware - you will probably need to insert break operations to make Switch/Case work the way you expect it to!

<?php

$ageGroup = rand(1,5); // Generate random age group from 1 to 5

switch ($ageGroup) { case (1): echo "You are in group 1"; break; case (5): echo "A member of group 5 no less!"; break; default: echo "Some other group namely $ageGroup"; break; }?>

Control Structures - SWITCH

27

<HTML> <BODY> <H2> Using the for loop </H2> <?php for ($counter = 0; $counter < 5; $counter++){ echo "<BR>I'm doing this five times.<BR>"; echo "That was #" . ($counter + 1); } if ($counter > 4) echo "<BR>I guess that worked OK!"; ?> </BODY></HTML>

for ( startValue; loopLimit; loopIncrement )The FOR statement is very similar to RPG's

Note the use of { ... } to group the multiple statements to be executedWe will study the ForEach "automatic looping" array function later

Control Structures - FOR Loops

28

Page 15: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Simple arrays can be defined in a number of waysThe array() function shown below is very easy to use

It certainly beats compile time tablesAlternatively elements can simply be added as in the C++ example

The value will be placed in the next empty location (i.e. the 6th element)The array will dynamically expand if needed

Or loaded into a specific location as with ”CL” belowNote: Indexes start at 0 (zero) - not 1 as in RPG!

<?php

$languages = array("RPG400", "RPG IV", "COBOL", "Java", "PHP");print_r($languages); // print_r is a very useful debugging tool

echo "<br><br>";$languages[] = "C++"; // This adds another element to the array$languages[6] = "CL"; // And this loads "CL" into location 6

Var_dump($languages); // var_dump is also useful in debugging

?>

Arrays

29

Array ( [0] => RPG400 [1] => RPG IV [2] => COBOL [3] => Java [4] => PHP )

array(7) { [0]=> string(6) "RPG400" [1]=> string(6) "RPG IV" [2]=> string(5) "COBOL" [3]=> string(4) "Java" [4]=> string(3) "PHP" [5]=> string(3) "C++" [6]=> string(2) "CL" }

Output Result

Note: Neither print_r or var_dump produces output that is quite this "pretty" - I cleaned it up for the chart.

30

Page 16: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

ForEach() is a commonly used construct in PHPIt provides a simple means to iterate step though the elements of an array

Each active element in turn is assigned to the variable• ForEach ($arrayName as $currentElement) { actions };

You can then perform whatever processing you wish• Frequently used to iterate through results sets

<?php$languages = array("RPG400", "RPG IV", "COBOL", "Java", "PHP");

foreach ($languages as $language) { echo "<p>$language</p>"; }?>

Output of this code is:RPG400RPG IVCOBOLJavaPHPC++

More on Arrays - ForEach

31

NotesArrays are one of my favorite features in PHP. The flexibility afforded by being able to store any kind of data in an array element - including another array - makes for a very flexible and powerful programming environment.

ForEach is the easiest way to loop through an array and process the contents. You don't need to concern yourself with the number of elements etc. because PHP will skip "empty" elements and automatically stop when the end of the array is reached.

32

Page 17: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

More on Arrays - Other Functions

foreach is just one of over 60 array functions• You can sort arrays

! In strict code sequence or in natural sequence• You can search them• Create an array from a text file

! Or create a text file from an array• Compare two arrays for differences, or matches, or ...

Array can contain other arrays• That’s how 2 and 3 dimensional arrays are created

Arrays have a cursor and can be navigated like a file • Examples on the next page

33

NotesThere are some incredibly powerful array functions available in the standard PHP function library. Also as you will learn, arrays are at the heart of almost everything we do in PHP from database access, to web services, to calling RPG programs!

Take the time to thoroughly explore PHP's arrays - it may also give you some ideas on how to further exploit arrays in your RPG code!

34

Page 18: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

PHP provides many array navigation functions• They greatly simplify array handling compared with RPG

Examples include: • current( ), next( ), prev( ), and end( )

Allows you to operate on an array as if it were a database

Navigating Arrays

<?php $myArray = array("Car", "Truck","Motorcycle","Plane","Bicycle"); echo "<p>Here's the complete array </p>"; print_r($myArray); echo "<br><br>"; echo "Current: ", current($myArray), "<br>"; echo "Next: ", next($myArray), "<br>"; echo "One after that: ", next($myArray), "<br>"; echo "Prev: ", prev($myArray), "<br>"; echo "End: ", end($myArray), "<br>"; echo "Resetting the array.<br>"; reset($myArray); echo "Current: ", current($myArray), "<br>";?>

35

Here's the complete array

Array ( [0] => Car [1] => Truck [2] => Motorcycle [3] => Plane [4] => Bicycle )

Current: Car

Next: Truck

One after that: Motorcycle

Prev: Truck

End: Bicycle

Resetting the array.

Current: Car

Output Result

36

Page 19: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

A really powerful featureAllows you to associate a key with an array elementElements can then be accessed via their key or sequentially

See the highlighted line for an example of keyed access

To do this in RPG requires matched arrays and a lot more codingAnd we don't have the same range of functions available in PHP

Associative Arrays are commonly used in PHP interfacesFor example database, web services, and most IBM i functions

<?php$programmers = array("RPG IV" => 600, "COBOL" => 99, "Java" => 195, "PHP" => "lots of");print_r($programmers);echo "<br><br>";foreach ($programmers as $programmer => $count) { echo "<p> There are $count $programmer programmers</p>"; }echo "<p>I repeat there are " . $programmers["RPG IV"] . " RPG IV Programmers</p>"?>

Associative Arrays

37

Output Result

Array ( [RPG IV] => 600 [COBOL] => 99 [Java] => 195 [PHP] => lots of )

There are 600 RPG IV programmers

There are 99 COBOL programmers

There are 195 Java programmers

There are lots of PHP programmers

I repeat there are 600 RPG IV Programmers

38

Page 20: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Agenda

Basic Web Input HandlingUsing $_GET and $_POST

Brief overview of i5 Features

A few examplesXML, Web services, email

39

<HTML><BODY><CENTER><H1>Reading All Form Data</H1><FORM GET ACTION="Pick-A-Food.php"> What's your name? <INPUT NAME="Name" TYPE="TEXT"><BR><BR> Select your favorite fruit(s): <SELECT NAME="Food[]" MULTIPLE> <OPTION>Apple</OPTION> <OPTION>Hamburger</OPTION> <OPTION>Pear</OPTION> <OPTION>Plum</OPTION> <OPTION>Pomegranate</OPTION> </SELECT> <BR><BR><INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Enter"></FORM></CENTER></BODY></HTML>

Basic PHP Web Input HandlingSubmitting data using a simple HTML Form• PLEASE don't use my "style" - everything here is squashed to fit!

ACTION identifies the script that will process the input• GET specifies the method by which the data is transmitted

! In this case it is appended to the URL and you can see it in the browser! With POST it would be sent in the background and would not be visible

40

Page 21: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

<HTML><BODY><CENTER><H3>Retrieving Data From Forms</H3><?php

$foodCount = count($_GET['Food']);

echo "<p>Your name is ", $_GET['Name'], " and you selected $foodCount different foods</p>";...

Retrieving Form Input DataInput method to be used (GET or POST) is specified on the Form• It determines which superglobal array will contain the data

! $_GET[ ] or $_POST[ ]! There is also $_REQUEST[ ] which contains the input from all sources

- But it represents a security risk and should only be used during testing

Our form used the GET method so all data is in $_GET[ ]• The count() function identifies the number of foods selected• And $_GET['Name'] retrieves the name entered

41

Connection ManagementConnect, change user, close….

DB2 UDB access via SQL and/or Record level I/OFetch as array, get field lengths, field type, ...Create records, update, set ranges, ...

Command, Program and Procedure callsSupports return values

Data Areas, Data Queues, Message QueuesCreate, read, write, delete...

System valuesIncluding date, time, …

Spool Queues and filesList, Read, ...

A list of i5 functions is supplied at the end of

the handout

i5/OS Extensions and Toolkit

42

Page 22: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

IBM i Record I/O Functions - Part 1

<html><body><h2>Simple Full Table Test - i5 Native APIs</h2><table border="1" width="670"> <tr> <th width="110">Customer</th> <th width="260">Name</th> <th width="240">City</th> <th width="60">State</th> </tr>

<?php

function iErrMsg($msgText) { errInfo = i5_error(); echo '<tr><td colspan ="4">', $msgText, '<br>Message code: ', $errInfo[0], ' IBM i Message No: ', $errInfo[2], '<br>Text:', $errInfo[3], '</td></tr>'; die();}

Note: This part is just HTML - no PHP yet

Report errors using IBM i specific information

43

IBM i Record I/O Functions - Part 2

$i5conn = i5_connect( 'localhost','USERNAME','PASSWORD'); if (!$i5conn) iErrMsg('Connection attempt failed');

$customerFile = i5_open("PARTNER400/PCUSTOMERS", I5_OPEN_READ);

if (!$customerFile) iErrMsg('Unable to open Customer file');

while ($row = i5_fetch_assoc($customerFile)) { echo "<tr><td>{$row['CUSTOMER']}</td> <td>{$row['NAME']}</td> <td>{$row['CITY']}</td> <td>{$row['STATE']}</td></tr>"; }

i5_close($i5conn);

?>

</table></body></html>

Close out the Table etc.

Loop through all records and add to HTML table

44

Page 23: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

This uses SQLite which is built into V5+ releases of PHP<?php$db = sqlite_open("db.sqlite");$query = sqlite_query($db, 'SELECT * FROM names WHERE salary > 45000 ORDER BY lastname, firstname');// Output table headersecho "<table border=1 width=40%> <th>Id<th>Name<th>Location<th>Salary";// Get results as an array with the column name as the index$result = sqlite_fetch_all($query, SQLITE_ASSOC);// Iterate through the retrieved rows and output row dataforeach ($result as $entry) { echo "<tr><td>{$entry['id']}</td> <td>{$entry['lastname']}, {$entry['firstname']}</td> <td>{$entry['location']}</td> <td align=right>{$entry['salary']}</td></tr>";}// Wrap up the tableecho "</table>";sqlite_close($db);?>

Simple Database Example

45

<?php

$Customers = simplexml_load_file("\\Partner400\\Customers.xml");

foreach ($Customers->Customer as $customer) { print "$customer->Company<br>";}?>

I will only provide a brief explanation of the RPG versionAt first glance both programs look very similar

But all is not quite as it seems

/Free XML-INTO company %XML( XML_Input1: 'doc=file case=any' );

for i = 1 to xmlElements; dsply company(i); endfor;

XML Processing in RPG and PHP

46

Page 24: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

D progStatus SDS D xmlElements 20i 0 Overlay(progStatus: 372)

D i s 10i 0

D customers DS Qualified D recordCount 3p 0 D customer LikeDS(customer) Dim(99) D customer DS Qualified D type 10a D company 32a D address LikeDS(address) D address DS D street 32a D city 24a D state 2a D zip 5s 0

D XML_Input1 256a Inz('/Partner400/Customers.xml')

The previous chart left out this part of the RPG versionRPG requires the data to be predefined - PHP does it dynamically

RPG IV XML Processing Example

47

<HTML> <BODY> <CENTER> <H1>Send me some email</H1> <FORM METHOD=POST ACTION="phpemail.php"> Please enter your message and click Submit:<BR> <TEXTAREA NAME="message" COLS="50" ROWS="5"> </TEXTAREA><BR> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> </CENTER> <BODY></HTML>

PHP’s simple means for adding e-mail to your web pagesOr for that matter from a batch script

Below is the simple form usedThe script is on the next page

Adding e-Mail to Your Web Site

48

Page 25: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

<HTML> <BODY> <CENTER> <H1>Sending email</H1> Your email was sent.<BR> <?php $result = mail("[email protected]", "Test PHP Mail", $_REQUEST["message"]); ?> </CENTER> <BODY></HTML>

Simple Script to Send e-MailThe mail() function is used to send the mail

The first parameter is the recipient's addressThe second is the subject lineAnd the third is the message bodyNote that it is retrieved from the FORM text area "message"

The SMTP server configuration detail is supplied in php.ini

• Or can be supplied dynamically by the script

49

Using Web Services - SOAP APIs

<?php

$input = 100;

$currencies = array('FromCurrency' => 'USD', 'ToCurrency' => 'CAD');

$wsdl = 'http://www.webservicex.net/CurrencyConvertor.asmx?wsdl';

$client = new SoapClient ($wsdl);

$result = $client->ConversionRate($currencies); $rate = $result->ConversionRateResult; $answer = $input * $rate;

echo "\$$input US is worth \$$answer Canadian at the current exchange rate of $rate";?>

PHP provides quick and easy access to Web Services such as this• It is a currency conversion service

• PHP's SOAP APIs make it simple to add such functionality to your scripts

50

Page 26: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Web Sites:www.zend.com

For tutorials, documentation, samples, forums, and all of the i5 specifics

www.php.netThe on-line PHP manual is amazing!

Books:PHP in a Nutshell - Paul Hudson - O'Reilly PressSpring Into - PHP 5 - Steven Holzner - Addison WesleyThe IBM i Programmer's Guide to PHP - Jeff Olen and Kevin Schroeder - MC Press

Other:Countless tutorials and samples on the web. Just "Google it"Load Zend Server Community Edition on your PC

http://www.zend.com/en/community/zend-server-ceAnd don't forget to download Zend Studio while you are there

Useful (hopefully) References

51

Agenda

?Please e-mail Jon at: Jon.Paris @ Partner400.com for any questions

The IBM i specific APIs described earlier and shown on the following pages will shortly be replaced. They are shown here just to give you an idea of the range of functionality.

The new version will offer all of the same facilities but in most cases will be simpler to use and exhibit improved performance.

52

Page 27: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Connection Managementi5_connectI5_pconnecti5_closeI5_pclosei5_adopt_authorityi5_errori5_errormsg

CL callsi5_commandi5_cmdget

Program Callsi5_program_preparei5_program_calli5_program_close

Data retrievali5_fetch_array i5_fetch_assoc i5_fetch_object i5_fetch_rowi5_infoi5_field_leni5_field_namei5_field_scalei5_field_typei5_list_fieldsi5_num_fieldsi5_result

i5_ APIs

53

i5_ APIs

Native file accessi5_openi5_addnewI5_editI5_deletei5_cancel_editi5_setvaluei5_updatei5_range_fromi5_range_toi5_range_cleari5_data_seeki5_seeki5_bookmark

Native file access (contd.)i5_free_filei5_new_recordi5_delete_recordi5_update_recordi5_get_keys

System Valuesi5_get_system_value

Data Areasi5_data_area_createi5_data_area_readi5_data_area_writei5_data_area_delete

54

Page 28: Programming With PHP: An Introduction for RPG Programmers … Presentations/PHP_IntroForRPGers… · Programming With PHP: An Introduction for RPG Programmers Your Partner in IBM

Data Queues i5_dtaq_openi5_dtaq_puti5_dtaq_readi5_dtaq_close

Jobs i5_jobs_joblogi5_jobs_joblog_closei5_jobs_list_starti5_jobs_list_close

Message Queues i5_messageq_openi5_messageq_close

Output Queues i5_outq_openi5_outq_readi5_outq_close

Spool File i5_spool_openi5_spool_get_datai5_spool_from_datai5_spool_from_filei5_spool_close

User Space i5_userspace_createi5_userspace_geti5_userspace_delete

i5_ APIs

55