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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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:

<HTML><HEAD><TITLE>PHP Example</TITLE></HEAD>
<BODY>
You are using <? echo $HTTP_USER_AGENT ?><BR>
and coming from <? echo $REMOTE_ADDR ?>
</BODY></HTML>


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 207.164.141.23

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:

<HTML><HEAD><TITLE>Form Example</TITLE></HEAD>
<BODY>
<H1>Form Example</h2>
<?
function show_form($first="", $last="",
$interest=""){
$options = array("Sports", "Business",
"Travel", "Shopping",
"Computers");
if(empty($interest)) $interest=array(-1);
?>
<FORM ACTION="form.php3" METHOD="POST">
First Name:
<INPUT TYPE=text NAME=first
VALUE="<?echo $first?>">
<BR>
Last Name:
<INPUT TYPE=text NAME=last
VALUE="<?echo $last?>">
<BR>
Interests:
<SELECT MULTIPLE NAME=interest[]>
<?
for($i=0, reset($interest);
$i<count($options); $i++){
echo "<OPTION";
if(current($interest)==$options[$i]) {
echo " SELECTED ";
next($interest);
}
echo "> $options[$i]\n";
}
?>
</SELECT><BR>
<INPUT TYPE=submit>
</FORM>
<? }

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


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
PHP ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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