Informing the IBM Community

How to get started with PHP on Power i

0
(0)

Abstract technology background with hexagonal shapes

Why would you choose to start using PHP on your Power i? Well, if you are thinking of delivering applications in a browser – either on a desktop PC or mobile device – PHP is an ideal choice. It is built for the web. In fact, vast swathes of the web are written using it. Ever heard of Facebook or Wikipedia?

This popularity means that there are plenty of resources online to help you too. The wonderful php.net is my first port of call when I need assistance.

Another plus: if you are an old school developer like me with functional programming skills built up from years of RPG coding, PHP is easier to pick up as it can be written functionally. Don’t get me wrong, you will be drawn into the dark world of object-oriented programming but PHP makes this transition far gentler.

What is needed?

If you have made the decision to take the plunge, what do you need to get started?

First, the server-side stuff. To run PHP on IBM i you will need to install Zend Server. This is the PHP engine that processes the code. Zend and IBM collaborated in bringing PHP to the i, so we’re fortunate to be able to use the basic edition of the software for free with the first year of support included.

Rather than parrot the comprehensive installation instructions provided by Zend, I’ll simply direct you to its website. Click the link to the Zend Server for IBM i Installation Guide, and you are on your way. The latest versions of Zend Server are available for IBM i 6.1 and higher. If you are still on 5.4, you’ll have to make do with the older Zend Server 5 and will miss out on some of the latest features like Z-Ray.

Pre IBM i version 5.4?  Well, you know what you need to do, don’t you? I’m sure IBM will be happy to help you out with that long-overdue upgrade.

Once Zend Server is in place, you are ready to start writing PHP code. But how? In theory, you could create and edit source files directly in the IFS but this is likely to lead to a whole world of pain, mainly due to character-set issues. Plus you will be missing out on loads of functionality provided by modern PC-based tools.

You could use a text editor on your PC and use an FTP tool like FileZilla to upload source to the server but there are much better options in the form of modern integrated development environments, or IDEs. If you are looking for a quick-start option, WebSmart PHP from BCD Software provides a fully integrated IDE built specifically for IBM i. On top of this, WebSmart features a whole bunch of templates that allow you to develop fully-functional PHP programs in a matter of moments, including ones designed specifically for mobile. For more information, visit the BCD UK website.

Alternatively, if you want to start coding from the ground up, Zend has a tool for the job – Zend Studio. Once again, because of the relationship between Zend and IBM, as an i user you get this for free along with a year’s support and upgrades. If you have previous web development experience using Eclipse or you use the Rational IDE, Zend Studio should look pretty familiar. This is because all these tools are built on the same technology.

You can download Zend Studio from the Zend website here.

Your first PHP program

For my first example program, I am going to use Zend Studio. If you want to play along, download it using the link above, install it, fire it up and let’s go.

Zend Studio provides a whole load of functionality to help you develop web applications, most of which would just be confusing for a beginner. So let’s start with that most basic of basics, the Hello World program.

Zend Studio uses perspectives to provide different views for different circumstances. As we’re writing a PHP program, select the PHP perspective by clicking Window > Open Perspective > PHP.

We start by creating a new project in our Zend Studio workspace. Projects allow us to group together all the elements of a web application and also provide access to useful features like automatic upload onto the server.

Click File > New > Local PHP Project

Your project needs a name, so call it “Hello World”. Leave all other settings alone and click “Finish” (see screenshot below).

Creating the PHP file

In the PHP Explorer pane you should now see your new project. At this point, the project only exists on your PC, so we need to link it to a location in the IFS of the server so that we can run our PHP there.

Right-click on the project in the PHP Explorer pane, then click on “Properties”. This opens the Properties window for the project. Click on Automatic Upload to specify the server destination for our project.

Check the “Enable automatic upload” box, and ensure the Remote Connection option is selected. The remote connection describes our server, so we’ll need to create one. Click on “Manage” to do this. You should now see the Remote Connection Profile window. Click “Add” to create a new connection.

In the New Remote System Connection window, enter the Host Name or IP address of your IBM i.  System Type should be set to FTP Only unless you have SSH enabled on your server. Click “Finish” to create the connection.

Back on the Remote Connection Profile window, pretty much everything defaults in but you will need to specify the user name and password to use when connecting to the IBM i. Also, the Initial Directory should be set to the document root folder of Zend Server. If you have installed a box-fresh version of Zend Server, this will be “/www/zendsvr6/htdocs”.

Click the “Test Connection” button to check that the profile works, then click “Finish” to create the profile.

Back in the automatic upload properties window, make sure your new Remote Connection is selected. Leave the Application Directory as “/”, and the Upload Files setting as On Save.
Click on “OK” to save the automatic upload properties. You will be prompted for your IBM i user and password to validate the connection and also asked if you want to upload to the server at this time.  Click “No” (as we don’t have any files yet!).

Finally, we can now create our first PHP script. Right-click on the project in the PHP Explorer pane and click New > PHP File.

In the dialog box set the File Name to HelloWorld.php then click “Finish”.

In the main pane, the new file will be opened for editing, with a handy “<?php” tag inserted to start us off. On the next lines, enter the following:

$myText = "Hello World!";
echo $myText;

Then press Control-S to save the file. The file will be immediately uploaded to the server.

Finally (drum roll), open a browser and enter the following URL:

https://:10080/HelloWorld.php

where <myServer> is the Host Name or IP of your server. Hit “enter” and marvel at the results (see below).

Finished program

Congratulations, you’ve taken your first step down the road to PHP development. Give yourself a pat on the back.

Next steps

But what next? PHP can take you in some interesting directions and in terms of developing software; this introduction is literally the snowflake on top of the tip of the iceberg. What we’re doing here is generating some basic and structureless HTML which the browser is displaying.

If you are planning to develop web applications, a good next step is to gain a working understanding of HTML, CSS and JavaScript.  This sounds daunting but, fortunately, there are a lot of resources out there and the bonus is that the knowledge you gain is relevant for developing web apps using other languages too.

In addition to writing your own applications, PHP on IBM i also opens up the possibility to use popular open source products. Our website at Proximity  runs on our Power i using one such tool – Drupal – and there are many others that could be of value to your organisation. Thanks to Zend DBi – Zend’s implementation of MySQL on the IBM i – you can run pretty much any PHP-based software.

So whether it’s new software, free applications or a new front end for legacy software, PHP is a great way to move into the world of web on your Power i.

Pete Samways is a development manager at Proximity. He is an expert in the development, modernisation and mobilisation of IBM i applications, particularly in the logistics sector. He is responsible for delivering projects on behalf of Gebrüder Weiss and Agility amongst others. You can follow Pete on Twitter @pete_samways.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.