Chapter 1. PHP Review
In This Chapter
Overview
This book assumes that you have some experience with PHP. This book is not meant to be an introductory text on PHP programming but is instead a book on some of the more advanced uses for PHP that you won't find in many of the introductory books or tutorials on the Web.
Having said that, I am still providing here a brief introduction to PHP for those who may need a quick refresher or are already familiar with a similar language, such as PERL.
While Loops
While loops are another useful construct to loop through data. While loops continue to loop until the expression in the while loop evaluates to false. A common use of the while loop is to return the rows in an array from a result set. The while loop continues to execute until all of the rows have been returned from the result set:
while($row = mysql_fetch_array($result)) { // do something with the resulting row}
You can also use the continue command to skip over the current iteration of the loop, then continue. For example:
while($row = mysql_fetch_array($result)) { if($row['name'] ! = "Jet")) { continue; } else { // do something with the row}
Do While Loops
Do while loops are another useful loop construct. Do while loops work exactly the same as normal while loops, except that the script evaluates the while expression after the loop has completed, instead of before the loop executes, as in a normal while loop.
The important difference between a do while loop and a normal while loop is that a do while loop is always executed at least once. A normal while loop may not be executed at all, depending on the expression. The following do while loop is executed one time, printing out $i to the screen:
$i = 0; do { print $i;} while ($i>0);
Whereas the following is not executed at all:
$i = 0; while($i > 0) {print $i;}
Quite frankly, I never find much use for do while loops, but you may find an odd problem that requires it.
User-Defined Functions
In addition to PHP's built-in functions, you can create your own functions. Remember that if you want to use variables that exist outside of the function, then you must declare them as global variables or assign them as arguments to the function itself.
function check_age($age) { if ($age > 21) { return 1; } else { return 0; }}//usage:if(check_age($age)) { echo "You may enter!";} else { echo "Access Not Allowed!"; exit;}
The function would be called from within your script at the appropriate time, using the following syntax:
$age = 10; check_age($age);// prints "Access Not Allowed!" to the screen
Object Oriented Programming with PHP
PHP supports Object Oriented Programming (OOP) in the form of classes. Just like in other OOP languages, the classes can be extended for greater reuse of code.
To create an address entry class that contains a person's first and last name and phone number:
class address_book_entry { var $first; var $last; var $number; function set_name($first, $last) { $this->first = $first; $this->last = $last; } function set_number($number) { $this->number = $number; } function display_entry() { echo "
Name: " . $this->first . " " . $this->last; echo "
Number: " . $this->number; }}//Usage:$entry = &new address_book_entry;$entry->set_name("Jane","Smith");$entry->set_number("555-555-5555");$entry->display_entry();//displays:Name: Jane SmithNumber: 555-555-5555
Additionally, you can extend an existing class to create a new class that has the same functionality of the old class, plus any new functionality you add:
class address_book_entry2 extends address_book_entry { var $email;function set_email($email) { $this->email = $email; } function display_entry2() { echo "
Name: " . $this->first . " " . $this->last; echo "
Number: " . $this->number; echo "
Email: " . $this->email; }}//Usage:$entry = &new address_book_entry2;$entry->set_name("Jane","Smith");$entry->set_number("555-555-5555");$entry->set_email("jsmith@com.com");$entry->display_entry();//displays:Name: Jane SmithNumber: 555-555-5555Email: smith@com.com
phpinfo( )
phpinfo() is a very useful function that allows you to see what version of PHP is running on your Web server, as well as all of the specific settings that are enabled in that version.
Throughout this book, there are sections that ask you to verify your settings. One way to do this is to create a script that includes just the following:
phpinfo();?>
Upon execution of the script, the browser displays the current PHP settings. This function is especially useful if you are working on somebody else's server and are not quite sure of its capabilities.
Additional Resources
If you still need additional information on the basics of PHP, the following books from Prentice Hall provide material suitable for the utmost PHP beginner:
Core PHP Programming: Using PHP to Build Dynamic Web Sites (2nd Edition) by Leon Atkinson. ISBN 0130893986.
Essential PHP for Web Professionals by Christopher Cosentino. ISBN 0130889032.
[1] Shameless plug for my own book, I know, but I've received a lot of email from nonprogrammers who said the book really helped!
PHP Syntax
PHP is a language that was designed to be easily embedded into HTML pages (although you don't have to do it that way). Most PHP pages have PHP code and HTML intermixed. When a Web server reads a PHP page, it is looking for two things to let it know it should start reading the page as PHP rather than HTML, the start and end PHP tags:
Variables
You can also easily intertwine small segments of PHP into HTML, such as when printing out the value of a variable:
Today's Date is = $date ?>.
The "=" syntax in the php, when followed by a variable, is used as shorthand for the echo() function.
You can include comments in your PHP scripts. Comments are ignored by the Web server, and any comments contained within the PHP code are not sent to a browser. There are three forms of comments:
Next page