Running RPG programs once they're created is easy. But what about PHP? We created a class-oriented script, but how do we run it? And what kind of setup is required to have a functioning PHP environment?
We spent the last few tips talking about classes and gradually building a small script that was based in a class. But have you run the script? Do you know how to run a PHP script?
PHP isn't like RPG. It doesn't just run on the machine that you create it on. And you don't just do a "callphp" or something like that.
Over the next few months, we are going to look at how to run a PHP script from a number of different environments. Now I think I need to say that none of these methods includes running a script from an RPG program. We will look at that later in the year. But before you are running things from RPG, you need to develop and test out your PHP scripts by themselves, and the techniques we will go over in the next few months will target that.
Client Side/Server Side
Remember that web languages come in two flavors: client side and server side.
Client-side languages (like HTML, for example) run on the client machine. In HTML's case, that means on the browser that is on your PC. You set up the script in your PC, and then you run it on your PC's browser with no additional software required. The browser interprets the HTML markup.
Server-side languages, however, do not run in your client machine. They run on the server, and the results of that run are then sent down to your client machine to be displayed.
PHP is a server-side language. So any script that you write on your PC will not run there but instead must be sent to a server for interpretation. So the first question is, how do you get your script to a server?
Well, you could set up or buy (through Network Solutions or GoDaddy or someone) a website that you could then upload code to test on a server, but that seems like a lot of bother. Fortunately, there is an easier way. All you have to do is get some software on your PC that will create an instance of a server on your laptop. Then you need to put your script in a place where that server can see it and run it. And voila, you are done.
What software can you use?
Again, the good news is that there are many products out there that will create a server on your PC and pick up and run PHP scripts.
Server Software Options
The original is the use of WAMP/MAMP/LAMP—one flavor for each of the three main operating systems: Windows, Mac, and Linux. (As you probably know, the acronym stands for Windows/Mac/Linux-Apache-MySQL-PHP.) You can download this onto your PC from a website for free and in a few minutes have a functional server running on your laptop. You can then create your scripts in a text editor and run them on the W/M/L server. Very simple yet very effective.
The other primary option is something that is popular for us IBM i'ers: Zend Server and Zend Studio. This is a combo product. Zend Server provides the server instance, and Zend Studio provides the IDE that you develop the scripts in. It is much more sophisticated and powerful than a text editor but also takes longer to figure out.
Over the next few TechTips, we will take a look at both of these options. I am going to start with WAMP/MAMP/LAMP because it is the easiest one to set up.
This software gives you a complete server stack defined by those products. You select the one for the platform you are on and install it from the web, and it will load an app onto your system that lets you start a local server that you can use for PHP testing.
Specifically, when you open the app, it will create a PHP stack and then open a browser page that will be a server browser page rather than one for your client machine. You can then enter the URL for your PHP script on that server browser page and it will run it.
There are a couple of ways to get the server software. One is to go to www.sourceforge.net and find WAMP, MAMP, or LAMP and then download. Sometimes, however, I find that site confusing, and the documentation is not always what I want it to be. It's sort of like going to a junk yard. The stuff is there, but you have to figure out where it is and how it works.
I am a Mac user, so I got my MAMP from www.mamp.info. They have a free version (MAMP) and a more sophisticated paid version (MAMP Pro) and all the info you need to download. Both editions provide MySQL, Apache, and PHP along with Python and Perl. Plus, if you have any questions, they have support (even for the free version) and will help you out. I have always used the free version and never needed the bells and whistles of the paid package. And their version of MAMP also runs on Windows (officially supported on 7.0, 8.0, 8.1, 10.0, and unofficially on XP). I would download from there. One thing to note is that it will be installed into a new directory (c:\MAMP), not into Program Files. There is a reason for this—some compatibility issue—but I am not going to go into that now. Or ever. 'Cause I don't care.
If you are on Linux, then I guess you are stuck with the junkyard.
Just follow the instructions, and you should be able to be up and running on this product (MAMP) in a very short time. Generally, starting this app will start MySQL and Apache and bring up a blank window on your browser that you will be able to test in. This is just a normal browser window except that technically it is running in your server instance rather than as a client browser session. Also, it is possible that when this window comes up it will say that the page cannot be found. Don't worry about this. It's not a real problem.
Setting Up Your Script
Once you get WAMP/MAMP/LAMP installed, now what do you do?
The first step is to create your script. If you are using W/M/L, then you can simply code it up in a text editor on your PC. You could use Notepad, but for a slightly more sophisticated experience, Google "Free text editors" and choose one that you can download and use. I use Text Wrangler, because it sounds kind of manly, and it works well for me.
Once you have keyed your script into the editor, the next step will be to save it. What is important here is that you have to put your script somewhere where the server can find it.
- For WAMP—c:\wamp\www\
- For MAMP (Mac) —/applications/MAMP/htdocs/
- For MAMP (Windows) —c:\MAMP\htdocs\
- For LAMP—/var/www/html (I am 90% sure this is correct)
Within the path, you can then create additional folders to organize your scripts.
Running Your Script
Once the script is positioned, running it is easy.
Start your server by using the icon that was installed as part of the download. This will start up the various services you need to define a server and display a web page that is running in the server environment.
To run your script, you will need to change the default URL that comes up with the server browser page to include the script name. If you have placed your script in the proper location as noted above, then all you need to do is append the script name to the "localhost:9999" verbiage. Be sure to start it with a slash (/) and include any qualifying folders if they are part of the script path.
So, for example, taking a Mac example, if the full path to the script is /applications/MAMP/htdocs/Project1/index.php, then to run that we would need to append /Project1/index.php after the local host:8888 that appears when the server browser page is displayed. Key that in and hit Enter and the script should run. If you have any echo or print commands in the script, then the output will appear on the web page.
And That's It
And that's all there is to it. This is one way to run PHP programs on your PC. It's simple, it's free, and you can do it today. Is it the best way? How would I know? Maybe not. So our next TechTip will start looking at using the Zend products: Zend Server and Zend Studio. Stay tuned.
MC Press Online