More Books
PHP 5 Unleashed
PHP 5 Unleashed
Table of Contents
Copyright
Lead Author
Contributing Authors
Acknowledgments
We Want to Hear from You!
Reader Services
Introduction
Organization of the Book
Part I. Working with PHP for General Web Development
Chapter 1. Basic PHP Development
How PHP Scripts Work
Basic PHP Syntax
Basic PHP Data Types
Variable Manipulation
Control Structures
User-Defined Functions
Dynamic Variables and Functions
Multiple File PHP Scripts
References
Strings in PHP
Comparing Strings
Advanced String Comparison
Search and Replacement
Formatting Strings
Strings and Locales
Formatting Date and Time Values
Summary
Chapter 2. Arrays
Basic Arrays
Implementing Arrays
More Array Materials
Chapter 3. Regular Expressions
The Basics of Regular Expressions
Limitations of the Basic Syntax
POSIX Regular Expressions
Perl-Compatible Regular Expressions (PCRE)
PCRE Modifiers
A Few Final Words
Chapter 4. Working with Forms in PHP
HTML Forms 101
Working with Form Submissions in PHP
Summary
Chapter 5. Advanced Form Techniques
Data Manipulation and Conversion
Form Data Integrity
Form Processing
Summary
Chapter 6. Persistent Data Using Sessions and Cookies
HTTP Cookies
PHP Sessions
Advanced Sessions
Summary
Chapter 7. Using Templates
The What and Why of Templates
The Smarty Template Engine
Summary
Part II. Advanced Web Development
Chapter 8. PEAR
What Is PEAR?
Getting and Installing PEAR
Using the PEAR Package Manager
Using the PEAR Website
Using PEAR Packages in Applications
Summary
Reference
Chapter 9. XSLT and Other XML Concerns
Relating XML to HTML
Using XSLT to Describe HTML Output Using XML Input
PHP4 and XSLT Using the DOM XML Module
PHP4 and XSLT Using the XSLT Module
PHP5 and XSLT
Accessing XML Data Using SimpleXML
Generating XML Documents Using PHP
Summary
References
Chapter 10. Debugging and Optimizations
Debugging Your PHP Scripts
Optimizing Your PHP Scripts
Summary
Chapter 11. User Authentication
Authenticating Users in PHP
Securing PHP Code
Summary
Chapter 12. Data Encryption
Shared Secret Versus Public Key
Shared Secret Algorithms
Public Key Cryptography
Using Public Keys in PHP
Summary
Chapter 13. Object-Oriented Programming in PHP
Why Objects?
Creating Basic Classes
Advanced Classes
Special Methods
Class Autoloading
Object Serialization
Exceptions
Iterators
Summary
Chapter 14. Error Handling
The PHP Error-Handling Model
What to Do About Errors
The Default Error Handler
Error Suppression
Custom Error Handlers
Causing Errors
Putting It All Together
Summary
Chapter 15. Working with HTML/XHTML Using Tidy
Introduction
Basic Tidy Usage
Tidy Configuration Options
Using the Tidy Parser
Applications of Tidy
Summary
Chapter 16. Writing Email in PHP
The MIME Protocol
Implementing MIME Email in PHP
Summary
Part III. Building Applications in PHP
Chapter 17. Using PHP for Console Scripting
Core CLI Differences
Working with PHP CLI
CLI Tools and Extensions
Summary
Chapter 18. SOAP and PHP
What Are Web Services?
Installation
Creating Web Services
Consuming Web Services
Looking for Web Services
Summary
Chapter 19. Building WAP-Enabled Websites
What Is WAP?
System Requirements
Introduction to WML
Serving WAP Content
Sample Applications
Summary
Part IV. I/O, System Calls, and PHP
Chapter 20. Working with the File System
Working with Files in PHP
File Permissions
File Access Support Functions
Summary
Chapter 21. Network I/O
DNS/Reverse DNS Lookups
Socket Programming
Network Helper Functions
Summary
Chapter 22. Accessing the Underlying OS from PHP
Introduction
Unix-Specific OS Functionality
Platform-Independent System Functions
A Brief Note About Security
Summary
Part V. Working with Data in PHP
Chapter 23. Introduction to Databases
Using the MySQL Client
Basic MySQL Usage
Summary
Chapter 24. Using MySQL with PHP
Performing Queries from PHP
A MySQLi Session Handler
What Is a Custom Session Handler?
Summary
Chapter 25. Using SQLite with PHP
What Makes SQLite Unique?
Basic SQLite Functionality
Working with PHP UDFs in SQLite
Odds and Ends
Summary
Chapter 26. PHP's dba Functions
Preparations and Settings
Creating a File-Based Database
Writing Data
Reading Data
Sample Application
Conclusion
Part VI. Graphical Output with PHP
Chapter 27. Working with Images
Basic Image Creation Using GD
Using the PHP/GD Drawing Functions
Working with Colors and Brushes
Using Fonts and Printing Strings
General Image Manipulation
Other Graphics Functions
Summary
Chapter 28. Printable Document Generation
A Note Regarding the Examples in This Chapter
Generating Dynamic RTF Documents
Generating Dynamic PDF Documents
Related Resources
Part VII. Appendixes
Appendix A. Installing PHP5 and MySQL
Installing PHP5
Installing MySQL and PHP Modules
Installing PEAR
Appendix B. HTTP Reference
What Is HTTP?
PHP Programming Libraries for HTTP Work
Understanding an HTTP Transaction
HTTP Client Methods
What Comes Back: Server Response Codes
HTTP Headers
Encoding
Identifying Clients and Servers
The "Referer"
Fetching Content from an HTTP Source
Media Types
Cookies: Preserving State and a Tasty Treat
Security and Authorization
Client-Side Caching of HTTP Content
Appendix C. Migrating Applications from PHP4 to PHP5
Configuration
Object-Oriented Programming (OOP)
New Behavior of Functions
Further Reading
Appendix D. Good Programming Techniques and Performance Issues
Common Style Mistakes
Common Security Concerns
Style and SecurityLogging
Summary
Appendix E. Resources and Mailing Lists
Relevant Websites
Mailing Lists and Newsgroups
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

Basic PHP Syntax

Now that you are aware of how PHP scripts are executed, let's discuss how to actually write your first PHP script. All PHP scripts are written in what are called code blocks. These blocks can be embedded into HTML, if desired, and are generally defined by <?php at the start and ?> at the end. Everything outside of these block identifiers will be ignored by the PHP interpreter and instead passed directly back to the Web server to be displayed to the client. Listing 1.1 is an example of a simple "Hello World" PHP script to get you started:

Listing 1.1. A Simple "Hello World" Script
<HTML>
<HEAD><TITLE>My First PHP Script</TITLE></HEAD>
<BODY>
<?php

       echo "Hello, world!"; 

?>
</BODY>
</HTML>

As you would expect, the first three lines of this simple PHP script are ignored and passed directly to the output of the script. The fourth line, however, is executed by PHP, and the string "Hello world!" is printed to the browser, followed by the rest of the ignored HTML text. You have learned your first PHP statementthe echo statement. This statement is the basic method in PHP to display content back to the client, and you'll be using it extensively throughout the book. Also note that, as with other C-style languages, each statement ends with a semicolon.

NOTE

Although <?php and ?> are generally used, the following are also valid code-block separators:

<? ... ?>

Shorthand version of <?php and ?>

<% ... %>

ASP style

<SCRIPT LANGUAGE="PHP">

...

 

</SCRIPT>

HTML editor compatible syntax


Note that some of these code block separators function only when the associated php.ini configuration directive is enabled. Unless there is a specific reason not to, using the default <?php and ?> tags is strongly recommended.


Although it's PHP, the preceding script does nothing that couldn't already be done with standard HTML. To do anything worthwhile, you'll need to learn how to use PHP variables.

In PHP, variables always start with the $ symbol followed by any combination of characters, provided that the first character following the $ symbol is a valid letter or underscore. Valid letters include uppercase and lowercase az as well as characters whose ASCII-value is between 127 and 255 (non-U.S. letters). In PHP, variables can be defined either by assigning them a value or by using the var statement. Listing 1.2 shows a few examples:

Listing 1.2. PHP Variable Examples
<?php

    $myvar = "foo";             /* Assigns the string 'foo' */

    badvar = "test";            /* Invalid, no $ symbol */
    $another(test)var = "bad";  /* Invalid, can't use () */
    $php5 = "is cool";          /* Correct Syntax */
    $5php = "is wrong";         /* Invalid, starts with number */
?>

NOTE

In PHP, everything between /* and */ is considered a comment used to describe the script and is ignored by the interpreter. For single-line comments, // or # may be used to "comment out" the remainder of a single line:

<?php
    $var = "foo";        // This is all ignored
    $var = "bar";        # so is this
?>


Although it is not necessary to destroy variables to free up resources (PHP's garbage collection routines will do so for you when a script terminates), it is sometimes desirable to force the destruction of a variable. To do this, PHP provides the unset() function. This function can be used on any valid PHP variable, including array elements (see Chapter 2, "Arrays," for an in-depth discussion of arrays). Listing 1.3 demonstrates its use to destroy an existing PHP variable:

Listing 1.3. Using the unset() Function
<?php
    $myvar = "This is a string";
    unset($myvar);        // Destroy the variable
?>

As far as variable types are concerned, PHP is classified as a loosely typed language. This means that a variable does not have to be defined as a string, an integer, a floating point, and so on. Instead, the variable is assigned a value and PHP will treat it accordingly, depending on the circumstance under which it was used. In PHP, there are three basic variables types (integer, string, and floating point) and two complex types (objects and arrays). This chapter will deal only with the basic types; see Chapter 2, "Arrays," and Chapter 14, "Object-Oriented Programming in PHP," for details regarding the remaining two complex variables.