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 Data Types

The first data type I'll introduce you to is the integer. The integer is the fundamental numeric data type in PHP and represents whole-number signed values up to a little over 2 billion. In practice, PHP will accept integer values using three mathematical bases: decimal (base 10), octal (base 8), and hexadecimal (base 16). In most situations, PHP scripts are written using decimal notation; however, in some situations octal or hexadecimal numbers make life easier. Listing 1.4 shows how each is represented in PHP:

Listing 1.4. Storing Integers in PHP
<?php

    $my_int = 50;       /* Standard Decimal Notation */
    $my_int = O62;      /* Same number, Octal Notation (starts with the letter O)*/
    $my_int = 0x32;     /* Hexadecimal Notation */

?>

When working with fractions, PHP represents the value using the floating-point data type. Floating-point numbers are defined as any number that contains a decimal fraction and that can be expressed in decimal or scientific notation, as shown in Listing 1.5:

Listing 1.5. Storing Floating Point Numbers in PHP
<?php

    /* Standard Floating Point Notation */
    $my_float = 5.1;

    /* Scientific Floating Point Notation of same number */
    $my_float = .051e2;    

?>

The final basic data type that is discussed in this chapter is the string. You have already been briefly exposed to strings when you examined the first real PHP example in this chapterprinting "Hello world" to the client; however, there is much more to strings than that simple example. To start off, there are two types of strings: parsed and unparsed. Parsed strings are defined using double quotes and are parsed by PHP, whereas unparsed strings are represented by single quotes and are taken as is. What does this difference mean to you as a developer? When a string is defined using double quotes (parsed), any references to variables within that string will automatically be replaced with their respective values, whereas unparsed strings will replace nothing. Listing 1.6 shows an example of both types of strings in action:

Listing 1.6. Parsed and Unparsed Strings in PHP
<?php

    $my_int = 50;
    $string_one = "The value of the variable is $my_int<BR>";
    $string_two = 'The value of the variable is $my_int<BR>';

    echo $string_one;
    echo $string_two;

?>

When this script is executed, the output will be as follows:

The value of the variable is 50
The value of the variable is $my_int

As you can see, although both strings were completely identical in content, $string_one was parsed by PHP and the reference to the $my_int variable was replaced with the value of $my_int. This is in contrast to the $string_two variable, which was not parsed by PHP, and the $my_int portion of the string remained as is.

NOTE

You might have noticed that both strings also contained an HTML <BR> tag. Because you are outputting your text to a Web browser, this tag is necessary to display the two strings on separate lines. Without this HTML tag, the strings would have been jumbled together on a single linehardly the desired result.


Along with the capability to replace variable references, parsed strings enable you to work with what are called escaped characters. This special format is used to represent characters that normally would be difficult or impossible to include within a string with a standard keyboard. For instance, consider a situation that required a double-quote character in a string that you would also like to be parsed. Because the double-quote character itself is used to define the beginning and end of the string, there is no inherent way to print the double-quote character itself. This is a prime example of when to use escaped characters. In PHP, the following escape characters are allowable, as shown in Table 1.1:

Table 1.1. Escape Characters in PHP

Escape String

Resulting Character

\n

Linefeed character

\r

Carriage return character

\t

Horizontal escape character escape character escape character escape character escape character tab character

\\

The backslash character

\$

The $ character

\'

The single-quote character

\"

The double-quote character

\###

ASCII character (octal)

\x##

ASCII character (hexadecimal)


Listing 1.7 is an example of escape characters in action; note the use of the \" sequence in the second variable assignment:

NOTE

The term ASCII stands for the American Standard Code of Information Interchange and represents a standard set of characters that any computer can understand. Although some characters in the ASCII set are no longer used (at least for their original purpose), ASCII is still a standard way to reference characters. More information on ASCII (including a complete table) can be found at http://www.asciitable.com/.


Listing 1.7. Using Escaped Characters
<?php

    /* Invalid string, won't work in PHP */
    $variable = "Do you know what "escaped" characters are?";
    /* The same properly formatted string*/
    $variable = "Do you know what \"escaped\" characters are?";
    /* Prints the 'a' character using hexadecimal */
    $variable = "\x41 is the 'a' character";

?>