A Simple PHP Program
Main | Static vs. Dynamic Websites | Using PHP | A Simple PHP Program | Variables and Operators | Practice 1 | Solution 1 | Loops | Practice 2 | Solution 2 | Functions | Practice 3 | Solution 3 | Conditionals | Practice 4 | Solution 4 Using Unix commands to move into placeWe will now create your first PHP program. This exercise will give a quick review of the relevant Unix commands involved in running PHP on blender, but our future examples will assume that you know these commands and focus on PHP only. If you have not yet done so, please SSH into blender.rutgers.edu and type the following commands at the prompt. Change directories so that you are in the php subdirectory that the "classprep" script created. cd public_html/php/ Create a file called "hello.php" inside this directory. touch hello.phpChange its permissions so that it can be seen from the web. chmod 644 hello.php Using EmacsEdit the hello.php that we just created using the emacs editor. emacs hello.phpThe editor should open an empty file named hello.php, in which you will write your program. Copy the following into this file: <html> <h3>My first PHP Program</h3> <? echo "Hello world!"; ?> </html> It is better that you copy the above in by hand (instead of copying and pasting) so that you get familiar with the syntax of PHP. Save the file in emacs by holding down the control key with your left hand and then typing x and then s with your right hand. You can then exit out of emacs in a similar way by typing control x and then control c. You can use the Emacs Command Summary or an introductory Emacs tutorial for basic information on Emacs. Emacs is much more than just a text editor. If you want a more extensive Emacs reference, see the GNU Emacs Manual. Seeing your example over the webOnce your file is saved you should try to run it on the webserver. In order to do this you must start up a new browser window and go to the appropriate URL. On a Unix flavored operating system (which is what blender runs) there are accounts. Each account has a uniqe email address and URL. URLs of the form (note the ~): http://blender.rutgers.edu/~username/ Point to the public_html directory of username. URL's of the form: http://blender.rutgers.edu/~username/php/ Point to the php directory under username's public_html directory. Since our examples are in your php directory please type (or copy and paste) a variation of the above URL (with your username) into a browser window to see your example run. When you get to the version of the above URL that is appropriate for you a listing similar to the following will be displayed on your browser.
Click on hello.php (not above, but on your browser). You should see something like the following.
If you don't see something like the above then there has been a syntax error. A syntax error is what happens when you do not write proper statements in PHP, and as a result the PHP interpreter, should tell you which statement it was not able to parse. Depending on how PHP has been configured on the computer that you are working on, you might see error messages or a blank screen. You can have a carefull look at the original code to the program as described above to try to figure what is wrong, but it is better for you to try to understand what the PHP parser is telling you about your errors so that you can learn to debug your own code. When your syntax is perfect your program should run as described above. Congratulations, you've written your first PHP script and have taken part in a traddition! The CodeNow that you have a feel for how to run PHP on blender, and have seen the output of your PHP program, let's analyze the code. The following is the combination of HTML and PHP that we used to write this program. <html> <h3>My first PHP Program</h3> <? echo "Hello world!"; ?> </html>If we remove the HTML we will have only the following PHP. <? echo "Hello world!"; ?>The above example revolves around the PHP language construct echo. The echo language construct must be followed by a space. After the space you can give echo any sentence you choose, provided that the sentence is contained within quotes (" "). Like any PHP statement, it must also end with a semicolon (;). StringsThe echo language construct also raises an important term in PHP, as well as general programming terminology: strings, words and sentences. In the purest sense, a string is series of characters. In the context of this example we will add the requirement that strings be contained within quotes (" "). Strings can be divided into two types, words and sentences.
Given the above three definitions, we can deduce that in the "Hello World!" example, what was referred to as a sentence, can also be formally refered to as a set of words, or a string. The following are examples of three different words:
and the following are examples of three different sentences:
All six of the above examples are strings. For more information on strings see the PHP manual's entry on strings. Now that we have our terminology defined we can say that whatever you choose to display with echo must be a string. PHP also has a a language construct called print which is very similar to echo. Within this tutorial we will use both echo and print. More information on echo and print can be found online. We can use echo to display information on our website. Most of the PHP programs we will examine in this class will use PHP to process data and come up with an answer. We will then use echo to display that answer. Thus, even though it serves a simple function, it is essential to using PHP.
jfulton [at] member.fsf.org
|