[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
a. php history
b. uses of php
* c. php hello world
d. installing php
2. http basics
3. php basics
4. php expressions
5. php client side
6. php flow control
7. php manual


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

Hello World

Hello World

Every programming language must have a Hello World exercise right at the very beginning of any form of instruction related to it. I am almost positive that this is inscribed in a mystical tome of computer science instructional guidelines somewhere. In any event, the objective of this exercise is to introduce you to the language and help you to develop a basic familiarity with using PHP on the context of Web documents.

In order to do this exercise, you are going to have to save the document to a directory accessible to a Web server that runs PHP. We will call this directory, your Web directory. It may be local, or it may require that you load your page out to a Web server somewhere. This depends on whether or not you have a test server with PHP running on the machine you are working on.

PHP runs as a process on an HTTP server, so you can't read PHP documents as normal files. Well, you can, but you will get the code instead of the results of the code, which is probably not what you wanted.

Step 1: Hello World

  1. Open up Notepad (or your text/code editor of choice) on your computer and type out the following code (almost) exactly as it appears. Which is to say type it as it appears except where it tells you to insert your name at that point, or where it tells you that you will be adding more content further on, etc.

    <?php 
    // we have to make our XML declaration this way or
    // the some servers will think it is a PHP command
    // instead of an XML command -- we will discuss how
    // to tell whether you have this problem later
      echo "<?xml version=\"1.0\" encoding=\"UTF-8\"\x3f>";
      echo "\n";
    ?>
    
    <!DOCTYPE html
      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    
    <html xmlns="http://www.w3.org/1999/xhtml" 
      xml:lang="en" lang="en">
    
    <head>
      <title>My First PHP Generated Document</title>
    </head>
    
    <body>
    
      <h2>My First PHP Generated Document</h2>
    
      <form method="post" action="hello.php"
      id="form1" name="form1">
        <p>Enter your name here:</p>
        <input type="text" id="namefld" name="namefld" />
        <p>Click this button to create a new page:</p>
        <input type="submit" value="Click Me!" />
      </form>
    
      <p>This page written by: 
      <cite>(your name here) </cite>
      <br />
      &copy; 2002 and beyond</p>
    
    </body>
    </html>
    
  2. Now save it in your Web directory with the name hello.php. If you are using Notepad, make sure neither you nor the computer add an additional .txt suffix to the end of the file name when you first save it.

  3. If you don't have PHP running on your computer, you will also need to load it out to a Web server with PHP to test it.

Step 2: Adding some PHP

Okay, so we have the XHTML shell and now need to add the PHP. PHP gets included inside SGML-style processing directives.

processing directive: a command directly addressed to the program processing the code to give it instructions on how to do so

All processing directives begin with an open bracket and a question mark, and end with a question mark and closing bracket. Within the brackets go the name of the application being passed instructions to and the instructions to be passed. If the name of the application is included, it must come immediately after the question mark, just like and HTML or XML tag.

<?appname some instructions ?>

We already have one example of this in the XML version declaration statement above.

The XML version declaration also used the processing directive format. However, some servers may have a PHP system variable called short_open_tag set to "on". This means that the PHP parser will treat any statement starting with an open bracket and a question mark as a PHP statement. Therefore, you cannot have any XML processing directives in the code if short_open_tag is set to on. Thus, in the example we are using PHP to write the XML processing directive out. We can do this because it is inside quotes and will be treated as a text string instead of a command.

Next we are going to do is add a piece of script that reads the information from the form when loading the page.

Wait, you may ask, if it processes the information before sending the page, how can it process information the user hasn't entered yet? The answer is, it can't. But it can look at the information send to it when someone fills in the form field and then clicks submit, which if you look at the action attribute value for the form, reloads the same page.

  1. Enter the following code right after the opening heading in the document.
    <h2>My First PHP Generated Document</h2>
    
    <?php
    if ($HTTP_POST_VARS["namefld"]) {
      echo "<p>Hello, ".$HTTP_POST_VARS["namefld"]."!";  
      echo "\n";
      }
    ?>
    
  2. Save the document again.

Step 3: Look at It

  1. Open up Internet Explorer or Netscape Navigator. In the address bar enter the address:
    [Your server and path]/hello.php
    and press Enter.

    If you are working from home and have PHP up and running, this should just be http://localhost/hello.php.

    If your Web page does not show up, check to see if you can figure out what is wrong. Otherwise ask for help.

    You should have something that looks like this:

    Sample Hello World Page
  2. Put your name into the name field and click the Click Me! button.

    The document should be rewritten with the greeting from the PHP code.

Explanation

What happened in this exercise?

When we submit our form it submits the information from the form back to the server. If you remember how forms work, they send a collection of name value pairs involving the name attribute of the field and the value entered into or assigned to the field.

The PHP parser recieves the information from the form formatted into an array. If we use method="post" then the array will be named $HTTP_POST_VARS. If we use method="get", then the array will be named $HTTP_GET_VARS. Here we used "post."

The index values for the array are the names of the fields in the form. Our field was named "namefld", therefore, the array element we are looking for is $HTTP_POST_VARS["namefld"]. We simply check to see if it exists, and if it does, we write its value out as part of a text string. The period, or dot, acts as a string concatenation operator in PHP. The echo command is used to write output back to the document being processed with PHP.

By the way, be careful with single and double quotes in PHP. They do different things.

[top]