Home arrow PHP arrow Page 8 - Sample Chapter: PHP Pocket Reference

Sample Chapter: PHP Pocket Reference

Never worked with PHP? Curious? In this article Tim takes a look at a sample chapter from O'Reillys PHP Pocket Reference. This chapter is a great introduction to PHP and is explained simply and clearly.

Author Info:
By: Tim Pabst
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
June 15, 2002
  1. · Sample Chapter: PHP Pocket Reference
  2. · Introduction
  3. · Installation and Configuration
  4. · Including Files
  5. · Arrays
  6. · Control Structures
  7. · Functions
  8. · Showing the Browser and IP Address
  9. · Web Database Integration
  10. · Conclusion

print this article

Sample Chapter: PHP Pocket Reference - Showing the Browser and IP Address
(Page 8 of 10 )

Here is a simple page that prints out the browser string and the IP address of the HTTP request. Create a file with the following content in your web directory, name it something like example.php3, and load it in your browser:

You are using <? echo $HTTP_USER_AGENT ?><BR>
and coming from <? echo $REMOTE_ADDR ?>

You should see something like the following in your browser window:

You are using Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
and coming from

Intelligent Form Handling
Here is a slightly more complex example. We are going to create an HTML form that asks the user to enter a name and select one or more interests from a selection box. We could do this in two files, where we separate the actual form from the data handling code, but instead, this example shows how it can be done in a single file:

<H1>Form Example</h2>
function show_form($first="", $last="",
$options = array("Sports", "Business",
"Travel", "Shopping",
if(empty($interest)) $interest=array(-1);
First Name:
<INPUT TYPE=text NAME=first
VALUE="<?echo $first?>">
Last Name:
<INPUT TYPE=text NAME=last
VALUE="<?echo $last?>">
for($i=0, reset($interest);
$i<count($options); $i++){
echo "<OPTION";
if(current($interest)==$options[$i]) {
echo " SELECTED ";
echo "> $options[$i]\n";
<INPUT TYPE=submit>
<? }

if(!isset($first)) {
else {
if(empty($first) || empty($last) ||
count($interest) == 0) {
echo "You did not fill in all the ";
echo "fields, please try again<P>\n";
else {
echo "Thank you, $first $last, you ";
echo "selected ". join(" and ", $interest);
echo " as your interests.<P>\n";

There are a few things you should study carefully in this example. First, we have isolated the display of the actual form to a PHP function called show_form( ). This function is intelligent in that it can take the default value for each of the form elements as an optional argument. If the user does not fill in all the form elements, we use this feature to redisplay the form with whatever values the user has already entered. This means that the user only has to fill the fields he missed, which is much better than asking the user to hit the Back button or forcing him to reenter all the fields.

Notice how the file switches back and forth between PHP code and HTML. Right in the middle of defining our show_form( ) function, we switch back to HTML to avoid having numerous echo statements that just echo normal HTML. Then, when we need a PHP variable, we switch back to PHP code temporarily just to print the variable.

We've given the multiple-choice <SELECT> element the name interest[]. The [] on the name tells PHP that the data coming from this form element should be treated as an auto-indexed array. This means that PHP automatically gives each element the next sequential index, starting with 0 (assuming the array is empty to begin with).

The final thing to note is the way we determine what to display. We check if $first is set. If it isn't, we know that the user has not submitted the form yet, so we call show_form( ) without any arguments. This displays the empty form. If $first is set, however, we check to make sure that the $first and $last text fields are not empty and that the user has selected at least one interest.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials