Course Links

Assignments

Resources

External

This lab will guide you through some initial exercises with PHP. Before you begin, you may want to review the lab on setting up your account on beowulf.

PHP Info

One useful function to try early on is called phpinfo(). The PHP file shown below contains nothing but a call to this function. You can paste it into a file called info.php and upload it to beowulf. Try it out and look at the results. It shows you all about how PHP is configured on a particular machine.

<?php
  phpinfo();
?>

Variables and Echo

Here is another short test file. Its purpose is to try out variables. Paste it into a file and try it out. Then play with the variables and the output statements a bit until you are sure you understand how everything works.

One point bears mentioning: the first two lines of the file tell PHP to display error messages instead of failing silently. This is very useful during site development, but you would not want these sorts of messages appearing in a production web site. For this reason they are usually turned off, but we want them on for our purposes. You should reproduce these two lines in all your pages. However, be aware that there are some sorts of syntax errors that will confuse the PHP parser and prevent execution of the commands that turn on error reporting -- so you still won't get any error message. In these cases you can use another file that turns on error reporting and then includes the suspect file. An example is shown following the program below.

<?php // turn on error display
ini_set("display_errors","On");
ini_set("display_startup_errors","On");

// here is the variable practice
$animal = "fox";

// difference between double and single quotes
echo "<p>This animal is a $animal.</p>";
echo '<p>You can call it $animal</p>';

// difference between value assignment and reference assignment
$animal2 = $animal;
$animal3 =& $animal;
$animal = "dog";
echo "<p>The quick brown $animal2 jumped over the lazy $animal3.</p>"
?>

Here is the error display file:

<?php // turn on error display                                                  
ini_set("display_errors","On");
ini_set("display_startup_errors","On");

include("file_to_test.php");
?>