Request for Question Clarification by
tar_heel_v-ga
on
14 Mar 2004 16:42 PST
Here is a sample exercise from week 2:
Hello WorldThe objective of this exercise is to develop a basic
familiarity with using PHP in the context of Web documents.
This should be a really easy assignment. If you have problems with it,
let me know because the homework will rapidly get much more
challenging. By the end of the semester, you should be able to take a
barebones description of the intended assignment, think through the
details and potential difficulties on your own, and produce a
solution.
Step 1: Hello World
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.
<THERE IS PHP CODE HERE>
Now save it in your homepage directory on your H:/ drive with the name
hello.php. Make sure neither you nor the computer add an additional
.txt suffix to the end of the file name.
If you are working at home, you will need PHP to test it or will have
to uplaod it to the campus servers first.
Step 2: Adding some PHP
Okay, so we have the XHTML shell and now need to add the PHP. PHP gets
included inside 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, the campus servers currently have a PHP system variable
called short_open_tag set to "on". This means that the PHP parser with
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.
What 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.
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 "</p>\n";
}
?>
Save the document again.
Step 3: Look at It
Open up Internet Explorer or Netscape Navigator. In the address bar
enter the address:
http://academ.hvcc.edu/~YourID/hello.php
and press Enter.
If you are working from home, then use the path appropriate for you
home server setup.
If your Web page does not show up, check to see if you can figure out
what is wrong. Otherwise ask for help.
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.
First, let's finish the directions, then I will explain what happened.
When you are done with this assignment, send me an e-mail with a link
to the page you have written. The address should be
http://academ.hvcc.edu/~YourID/hello.php
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 or $_POST for short. If we use method="get", then the
array will be named $HTTP_GET_VARS or $_GET. 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.
Each section has assignements. The text being used is "PHP and MySQL
Web Development, 2nd Edition. by Luke Welling, Laura Thompson.
Developer's Library/Sams Publishing, 2003. ISBN: 0-672-32525-X
Regarding Unix, it appears there is only a review and the rest of the
course doesn't focus on it.