PHP Tutorial - Page 1

Tutorial: Simple PHP Tutorial

If you have never used PHP before, this page gives some simple examples to get you started.  This page also gives a simple example of how to access a MySQL database from within PHP.

Using the CompSci web server at Biola

To publish a web page using the Computer Science web server, named compsci.biola.edu, you need to get an account on the server then put HTML or PHP files into the Sites directory.  To get an account, ask Dr. Lin or Matthew Weathers.  Once you have an account, you can use FTP to transfer files, or from a Windows computer, type in this address:

\\compsci.biola.edu\your_username\Sites

Your web pages will then be visible at http://compsci.biola.edu/~your_username

A Simple PHP Program

A PHP program is just an HTML file with some special PHP tags where the code goes.  The file should end in .php rather than .html, and it will have some PHP code between tags that look like this: <?php ... ?>.  Everything inside those tags is part of your PHP program, and gets run when the web server displays the page.

It's usually a good idea to create a template or sample web page in an HTML editor first, then add some PHP code later.  That saves you the task of writing HTML directly.  Let's make a list of squares.  In Microsoft Front Page or Macromedia Dreamweaver, we could make a web page that looks like this:

(See this web page at list_squares.html)  Once you create this web page, take a look at the source.  For this one, it looks like this:

<html>
<head>
  <meta http-equiv="Content-Language" content="en-us">
  <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  <title>List of Squares</title>
</head>

<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<font face="Lucida Sans Unicode">
<table border="0" width="100%" id="table1" cellspacing="0" cellpadding="0">
   <tr>
      <td width="40" bgcolor="#33FF66">&nbsp;</td>
      <td width="20" bgcolor="#3366FF">&nbsp;</td>
      <td bgcolor="#3366FF">
         <p align="center">
         <font size="6" color="#33FFCC">List of Squares</font>
      </td>
   </tr>
   <tr>
      <td width="40" bgcolor="#33FF66">&nbsp;</td>
      <td width="20">&nbsp;</td>
      <td><br>Here is a table of squares for you.
      <div align="left">
         <table border="1" id="table2" cellspacing="0" cellpadding="5" 
             bordercolor="#3366FF">
            <tr>
               <td><font color="#B8008A"><b>Number</b></font></td>
               <td><font color="#B8008A"><b>Square</b></font></td>
            </tr>
            <tr>
               <td bgcolor="#33FFCC">1</td>
               <td bgcolor="#33FFCC">1</td>
            </tr>
            <tr>
               <td bgcolor="#33FFCC">2</td>
               <td bgcolor="#33FFCC">4</td>
            </tr>
            <tr>
               <td bgcolor="#33FFCC">3</td>
               <td bgcolor="#33FFCC">9</td>
            </tr>
            <tr>
               <td bgcolor="#33FFCC">4</td>
               <td bgcolor="#33FFCC">16</td>
            </tr>
            <tr>
               <td bgcolor="#33FFCC">5</td>
               <td bgcolor="#33FFCC">25</td>
            </tr>
         </table>
         <p>The sum of all those squares is 55.
         </div>
      </td>
   </tr>
</table>
</font>

</body>
</html>
		

 Next, we will replace a part of the above HTML with some PHP code, which will allow the table to be created dynamically.  Save the HTML file as a PHP file by saving it with a filename with a .php extension.  For example, you could save the file as list_squares1.php.  Next, replace the inside of the table with some PHP code so that the table looks like this:

<table border="1" id="table2" cellspacing="0" cellpadding="5"
                 bordercolor="#3366FF">
  <tr>
    <td><font color="#B8008A"><b>Number</b></font></td>
    <td><font color="#B8008A"><b>Square</b></font></td>
  </tr>
<?php
  $sum = 0;
  $max = 10;
  for($i=1 ; $i <= $max; $i++)
  {
    $square= $i*$i;
    $sum = $sum + $square; 
    if($i%2==0)
    {
      $color = "#33FFCC";
    }
    else
    {
      $color = "#FFFFFF";
    } 
    print "<tr>
           <td bgcolor=\"{$color}\">{$i}</td>
           <td bgcolor=\"{$color}\">{$square}</td>
           </tr>";
  }
  print "<p>";
?>
</table>
<p>The sum of all those squares is <?php print $sum; ?>.

If you are familiar with C++ or Perl, you will recognize the code.  Notice that all variables begin with the $ sign, and they don't have to be declared.  Also, notice that the quoted string after the print statement has carriage returns in it, and the quote doesn't end until three lines later.

Here's the output of that script: list_squares1.php.  Notice that if you do View Source, you will just see the output of the PHP program, which is HTML, not the source program itself.  Here's a copy of the source program: list_squares1.php.txt.

A program isn't very interesting if it always does the same thing.  Next, we will look at how PHP can receive input from HTML forms.

Next: Part 2, Getting Input from Forms.

Part 1, A Simple PHP Program
Part 2, Getting Input From Forms
Part 3, Using PHP with Database Tables
  Part A, Creating a Table in MySQL
Part B, Reading Tables in PHP
Part C, Writing To Tables in PHP
Top of Page