CSC-111: Introduction to Information Technologypage 1

Lab 5: Basic PHP Tutorial

This tutorial provides an overview of the basic building blocks of the PHP language. Your task is to thoroughly study the code examples and experiment with them in order to understand the underlying concepts.

PHP examples

Some of the most common functionality of PHP is demonstrated in the examples below. All of the code in the grey boxes can be placed in a file with the extension .php and uploaded to a web server supporting PHP without modification. Your lab activity for today involves doing this to see what the code produces, then experimenting with the code and see what you can come up with. All code examples can be found in the OUT folder with the file name indicated. Additional examples of the same concepts can be found at the CSC-111 web site (cs.furman.edu/~ktreu/csc111/examples.html) and in the lecture notes.

Create a folder called lab5 in your csc111 folder on the G drive or on your USB drive. Put all of the files for this lab (and the postlab) in this folder. Then use WinSCP3 to connect to your CS department web server account and upload the entire lab5 folder into your www/csc111 directory.

All files containing PHP code should have the extension .php and PHP code should be in <?php … ?> blocks. Remember that none of this code will work unless it is first loaded to the CS department server. That is inherent in the concept of server-side scripting.

For each of the following examples, follow these steps:

  • Upload the code to the web server. (You just did this.)
  • Read the code and the description very carefully. Take your time. Ask questions. Make sure you understand what’s going on with the example.
  • Use your browser to try out the code and see what it does.
  • Use Dreamweaver or another text editor to make one or two (or more) changes to the example to demonstrate that you know what’s happening.
  1. Outputting data. (file: 1helloworld.php)


echo and print are interchangeable and can be used both with and without parentheses. They are used to write both content and markup (HTML tags) to the web page that is being created. Try printing a different message. Try printing the message in italics.


  1. Variables (file: 2variables.php)

There are two things to notice about variables. First, notice that all PHP variables begin with a $. This is a must; your code will not work without it. Second, notice that variables in PHP can accept any data type. Also notice that PHP uses a period for concatenation of strings. (In JavaScript it’s a plus sign.)

  1. Receiving Input and Conditional Statements (file: 3input.php)


In this example we review our HTML form skills, and see how to make decisions. Here’s what happened in the code. First we consulted a special variable called $_POST[‘name’] and saved its contents to a variable called $name. $_POST[‘name’] contains any value that has been sent to this script using a form field called name with the POST method. We saved it in the variable $name just for simplicity. Then we checked to see if the variable $name has a value. To do this we used an if statement. If $name is empty, we created a form for the user to enter a name. If $name has a value, we output it using echo. Notice that the else statement spanned over non-PHP code. Conditional statements can be opened in one block of PHP code and closed in another, which allows html code to be place between without the need for a bunch of echo statements.

If the information in the form were being sent with the GET method instead, the only change necessary would be to make $_POST[‘name’] instead be $_GET[‘name’].

An interesting aspect of this example is that the page that contains the web form is also the page that processes the form. There is nothing at all contradictory about this. It just requires the use of the if test to see if the form has been filled out yet.

  1. Looping (file: 4looping.php)

It is often necessary to perform repetitive tasks in PHP scripts. This uses a construct called a loop. Looping is handled in PHP exactly the same as it is handled in JavaScript and other languages like C/C++ and Java. for, while, and do..while loops are all included in PHP. Following is an example of a simple for loop. Another activity (on using PHP with MySQL) will give an example of a while loop.


In this example – which simply prints out “Hello, world” 10 times – the variable $i is called an index. It is initialized to 1 in the for statement. The value of the index is compared to 10 in the next part of the statement before each iteration of the loop. If it is less than or equal to 10, the loop continues. Otherwise it stops. At the end of the loop, $i is incremented by 1. This is caused by the $i++ statement. Experiment with different loop limits, different contents of the loop, etc.


  1. Using conditional statements to determine page content(file: 5ifelse.php)

This example demonstrates a powerful feature of PHP – the ability to dynamically select different content for a page. The code above checks the URL querystring (assuming the GET method) and, depending on the value of $_GET[‘body’], includes, or loads, another php file. After you upload this file, try these different URLs:

Everything after the question mark in each URL is called a querystring. It’s actually giving the page a value that it needs. You access querystring values using the $_GET array. Do you see how it works?


  1. Functions (file: 6adder.php)

Here we declared a function to do our work for us. This is done with the function keyword, followed by the name of the function and a pair of parentheses. The parentheses contain the parameters, or data that is expected by the function (which could be nothing). Note also that PHP functions require the use of the return command. This is how you send back a value to the place the function was called.

Here again we see the use of an HTML form, and a page that processes itself. Some things to experiment with:

  • Try making the program subtract or multiply instead.
  • Modify it to use the get method and $_GET array instead. (Then note how the querystring is used.)
  • Try having the addition done by a second PHP page. You might even find this simpler than the given example.

  1. Session variables (files: 7sessiontest.phpand7sessiontest2.php)


A more advanced feature of PHP is the session variable. A session variable is a special kind of variable that maintains its value from page to page (that is, as a browser clicks from page to page in the same browsing session).

In this example we see the creation of session variable count, expressed as $_SESSION[‘count’]. The build-in PHP function session_start() tells the PHP processor that session variables are being used on this page. The “++” code simply adds one to the value of the session variable. Thus the result you get when you reload the page.


The next example includes a second use of the same session variable. Its value is the same as it was on the page that linked to this one.

Session variables are extremely useful in certain situations. An example might be a site on which users have greater access to data if they have a membership of some kind. When they log in, a session variable can be used to indicate that they are a member, thus allowing them to browse the site without having to log in again every time they change pages.

By the time you’ve reached this point, you should thoroughly understand the examples we’ve covered. If not, go back and experiment some more with the code. Try different things to see what works and what doesn’t. Ask questions.

When you’re satisfied, go ahead and get started on the postlab activity.

More Information

The official PHP website.

Webmonkey: lots of tutorials

Devshed: more tutorials

PHPBuilder.net: even more tutorials

PHPCoder IDE