|PHP 1: What You Need to Know Before You Start|
|Programming - Web Languages|
|Written by David Shirey|
|Wednesday, 18 April 2012 00:00|
So you're a great RPG programmer. Is that enough? Maybe not.
So, here you are, an RPG professional, all dressed up and ready to go. You can code up RPG real good, even some ILE. Got all that CL and DDS stuff mastered, but darned if people aren't saying that isn't enough. You've got to expand your skill set they say, learn a Web language. Don't you know that everything's going to be on the Web next month?
There are a number of Web-oriented languages out there but, for my money, PHP is as good a place to start as any. It's sure as heck easier to learn than Java, so why not?
But where do you start? Normally I might say "with some simple code," but there are a few things you should know about PHP before you even do that because, in some ways, PHP is very different from RPG, and you have to understand those differences before you get in the water.
Getting Started with PHP
I guess I will start by saying that PHP is not that hard. But then, it's not that easy either. I mean, what do you want me to say here? If I say it's hard, then some people won't even try it. If I say it's easy, then the first roadblock that folks come to will overwhelm them because their expectations were so high for breezing through this. PHP can do some very complex things. And you can approach PHP from an object-oriented point of view. But you can also do a tremendous amount in PHP with just a small set of commands that are used in a procedural manner.
So, if PHP is different from RPG, how is it different? Let's take a look.
Well, for starters, you don't compile PHP. It's an interpretive language, and it's rendered on the fly when you process the page that contains PHP code in the Web server. This makes some things simpler. There is no "PHP Compiler," so it doesn't have to be installed on your IBM i. The rendering engine that interprets the PHP instructions is a standard (and pretty much universal) part of every browser, so it's always there waiting for you.
Of course, that also means that when you write it, there's no step that says "And here are all the syntax errors you need to correct." Instead, your PHP just won't work. The Web page will still display, but what you're trying to get to happen with the PHP won't happen. Now don't freak out; there are ways to debug code in PHP, good ways, but a syntax compile isn't one of them (although if you get the right code editor, that can really help).
And that brings us to the second thing. You write your PHP in a text file using a text editor, not in PDM or the other tools we use to write RPG and CL code. That is, there is no PHP source file or type on the i, although if you own Zend, it has a Windows-based IDE that runs on the i, which lets you create your programs there.
Instead, you'll create your PHP program on a text editor and it will be a text file, although instead of having a .txt extension, it will have a .php extension. You can use any text editor, even Notepad, but if you get something a bit more sophisticated, then it can actually help you with your writing (by automatically adding ending tags and such) and do some syntax checking.
PHP Happens in a Web Page
The PHP language is used to do something to a Web page. It may change the way it looks, it may get information from a data base and display it on the page, it may edit data and then write it to a data base, it can do all kinds of things. But it always does everything within the context of an HTML Web page.
To begin with, we'll actually embed the PHP code we write into an HTML Web page. That is, even though the file that you create to hold this program has a .php extension, you don't just create a PHP program. You create an HTML page, and in that page you embed PHP code. And then you give that HTML document a .php extension when you save it.
As you become more sophisticated with PHP, it's good form to actually put all of the PHP code in a .php file and then reference that file from the pure HTML (.htm or .html) file that describes the page. Either way, you can't just have a PHP file go to work; it is always within the framework of an HTML document.
Can you guess what I'm going to say next? Well, if you need to build your PHP code inside an HTML document, then you'll also need to learn at least a little HTML, hopefully HTML5. And, to be cool and display good Web form, you'll also have to learn a little CSS, preferably CSS3.
Wait a minute; don't walk away yet. It's not as bad as it sounds. In reality, it's quite possible that someone else on your team will do the HTML and CSS3, and just let you write your PHP code when they're done. Maybe. Sure, a definite maybe. In fact, let's assume that's going to be the case, and if it isn't, you can be surprised later. Bottom line, we will only learn enough HTML to set up a very simple document. But just be aware that the more HTML and CSS3 you know, the better off you'll be.
Server Side, Client Side
RPG runs on the i. But PHP runs on the Web, within the Web server that holds your Web pages. And that, where on the Web it runs, is a very important point. PHP is a server-side language.
HTML is a client-side language. It runs in the browser you're using. Make a change to the HTML code and run it in your browser and you see the change instantly. You don't need to actually upload that changed page to the Web server; you can see the change on your browser immediately (although no one else will see the change until you upload).
But a server-side language executes in the Web server where your pages are really being stored. And that means that if you write a PHP program and then make a change to it, you can't see the change simply by looking at that Web page in the browser on your PC. You need to have access to a true Web server to test the change.
And the reason I'm telling you this? Because when you start to write a PHP program, you won't be able to just sit down at your PC and test it. HTML runs on the client side, so you can immediately see what happens when you make an HTML change. But it's a little more difficult with PHP. So, what can you do?
Well, one way you can test your PHP programs is to actually move them to the Web and then test them there. That's not completely crazy; many companies have both a test and a production Web site, but it gets a little annoying to have to load things out to the Web every time you want to check something out.
The way most people do it is to run a server instance on their PC—that is, a copy of Apache to simulate a server environment. WAMP (Apache, PHP, and MySQL for Windows) and MAMP (Apache, PHP, and MySQL for Mac) are two of the packages that you can download to your PC for free to do this.
Or—and you can probably guess this one too—you can use Zend. One of the extra things Zend does is provide a built-in Apache server, etc. so that you can create your PHP program and test it within the same single environment.
To Zend or Not to Zend
And speaking of Zend, let's just say a couple of things.
The first thing that people want to know is if Zend is free with the i. The answer is complicated but basically boils down to no; it's not free. You have to pay for Zend. You see, the Zend product consists of two parts: the Zend Server or Zend Core (which comes in two flavors: the full blown Zend Server that costs money and the Zend Server Community Edition, which is Zend Server Lite, but it's still quite functional and it's free) and the Zend Studio, which you always have to buy. So, no matter how you slice it, you have to pay something to use Zend.
The second question is, do you need to have Zend installed in order to do PHP? And the answer to that is no, but certainly life is a lot simpler if you have Zend. For example, the Zend Server (including CE) comes with some extra IO routines that you can use to access both SQL and DB2 databases beyond what is in native PHP.
The bottom line is that you can get started with PHP right away, without having to convince someone to buy the Zend products. But you'll find life a lot easier if you download the free CE version of the Zend Server and take advantage of the features it offers. You can get it from the Zend Web site (www.zend.com). And the first thing you should do is get a little familiar with the PHP language.
Fortunately, there are a lot of resources out there if you want to learn PHP. Two that I think are particularly good are The IBM i Programmers Guide to PHP by Jeff Olen and Kevin Schroeder from MC Press (which also gives you an intro to Zend) or an online class Practical PHP Programming presented by TuxRadar (which is primarily oriented to Web programmers and doesn't mention the i ,but it's broken down into bite-sized bits for easy swallowing, covers more of the detail of the PHP language separate from the i, and is very good).
That might be a good place to start, and in our next installment (two months from now), we'll look over the basics of creating a simple PHP program within an HTML page. Excitin', ain't it?
|Last Updated on Wednesday, 18 April 2012 00:00|