CSC-111: Introduction to Information Technologypage 1

Lab 3: Basic PHP Tutorial, Part 1

This tutorial provides an overview of the basic building blocks of the PHPserver-side scripting 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 to understand it thoroughly. 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 ( and in the lecture notes.

Create a folder called csc111on your USB drive or on the G drive and create a folder called lab3 inside of it. Put all of the files for this lab (and the postlab) in this folder. Then use WinSCP to connect to your CS department web server account and upload the entirecsc111 folder into your www directory. You will then be able to run all of the files using the URL

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 the suggested changes to the example to demonstrate that you know what’s happening.
  • Remember that after making the changes you have to upload the file using WinSCP again.
  1. Pre-lab exercise.

Your first task is simply to make a convenient index page for your lab3 folder. Use Dreamweaver to make a new file called index.php and save it in your lab3 folder. Title the page “CSC-111 PHP Lab” and create a bulleted list of links to the files that you just uploaded. Upload the index page to your lab3 folder on the server with WinSCP. Then use your browser to test the page and make sure all of the links work. When they do, proceed with the following tests and experiments.

  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.


  • Modify the script so it prints a different message.
  • Now make the page print the message in bold. (Use the HTML <b>…</b> tags as part of what you print out.)
  • Add a second echo statement that “prints” an image. That is, echo the HTML that will display an image on your page. (If necessary, look at the HTML for one of your old pages to remember how the <IMG> tag works .)
  1. Variables (file: 2variables.php)

There are a few 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. Third, notice that we assign a value to a variable using the equals sign. The value that we want to assign to (i.e., store in) the variable goes on the right of the “=”. The variable is always on the left.


  • Use this web site -- -- to try different color codes as the value for the $color variable.
  • Add two new variables $first and $last, and set them to your own first and last name. Remember that any time you use text in a PHP program you have to surround it with double quotes. So I would use the following statement for this task:

$last = “Treu”;

  • Use echo statements to print out your full name before the “Hello, world” message. So the output doesn’t run together, you should also echo out both a space between your first and last name, and an HTML <p> or <br> tag after your last name.
  • Change the existing echo statement so it says hello to you by name, in your chosen color. You can either incorporate your $first and $last variables (along with a space between your names) into the existing echo statement, or you can make a new variable that stores your full name and use that instead. Try it both ways. Which way is easier?

  1. Making Decisions with Conditional Statements (file: 3decisions.php)

This code begins by finding the current time and the hour (using military time) and assigning those values to variables. (It does this using a built-in PHP function called date(). We will work with functions next week.) It then makes a decision about what to print based on the current hour. Notice how the choice is made using two comparison tests (or conditions), followed by a default condition. Notice also how the action to be taken for each condition is indicated in PHP by curly brackets.


  • Try setting the hour manually to different values between 0 and 23. That is, remove the date(“G”) function and replace it with a specific number. Make sure to test each of the three possible conditions.
  • Modify the PHP code to include a “Good night!” option. This should include any hour between 9 p.m. (21 in military time) and 4 a.m. Note that this will include two new conditions in your code. Test your code again to make sure it works properly. Then set the assignment of the $hour variable back to the way it was when you started.
  1. Receiving Input and More Conditional Statements (file: 4input.php)

In this example we review our HTML form skills from last week’s lab, and see again how to make decisions. Here’s what happensin the code. First we consult a special variable called $_POST[‘username’] and save its contents to a variable called $inputname. $_POST[‘username’] contains any value that has been sent to this script using a form field called username with the POST method. Then we check to see if the variable $inputnamehas a value. To do this we use an if statement. If $inputname is empty (has no value), we created a form for the user to enter a name. If $inputnamedoes have 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.

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.

Finally, notice the use of concatenation in this example. Concatenation is an operation which combines multiple strings of characters into one. In PHP, a period indicates concatenation. For example, if we have the variable

$string1 = “ABC”;

then the result of the statement

echo $string1 . “XYZ”;

would be to print “ABCXYZ”.


  • What happens if you change the “action” of the form to something different?
  • What happens if you change the name of the text box in the form to something other than “username”?
  1. Looping (file: 5looping.php)

It is often necessary to perform repetitive tasks in PHP scripts. This uses a construct called a loop. Following is an example of a simple for 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.


  • Change the code so the index itself is echoed inside the body of the loop.
  • Create a new variable called $limit and assign the number 10 to it before the loop. Change the loop so it uses this variable instead of the number 10 as it does now.
  • Change the value of the variable to see how it changes the operation of the loop.

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 even more tutorials