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 place

We 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.php
Change its permissions so that it can be seen from the web.
   chmod 644 hello.php

Using Emacs

Edit the hello.php that we just created using the emacs editor.

   emacs hello.php
The 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 web

Once 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.

listing of php dir

Click on hello.php (not above, but on your browser). You should see something like the following.

hello.php's output

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 Code

Now 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 (;).

Strings

The 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.

  • A word is a series of characters within quotes (" ") that does not contain any spaces or commas.

  • A sentence is a series of characters within quotes (" ") that does contain at least one space or comma.

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:

  1.  "1234567890"
  2.  "-=\_+|X~qwertyuioo"
  3.  "String"

and the following are examples of three different sentences:

  1.  "1234567890,-=\_+|X~qwertyuioo"
  2.  "pasdf ghjkl;;'zxcv     bnm,./[]{}"
  3.  "This collection of words is a string!"

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
22 Aug 2013