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

Variable Manipulation

Now that you have been introduced to the basic PHP data types, let's explore how these data types can be manipulated to perform calculations and more using PHP. As you would expect, PHP supports all the basic mathematical operations of any programming language, including addition and multiplication as well as a wide range of trigonometric and logarithmic functions. Beyond mathematical manipulations, PHP supports an even greater amount of string manipulation functions. This chapter will cover only the more fundamental variable manipulations used in both strings and mathematics.

Performing mathematical operations in PHP is a fairly intuitive task. PHP supports all the common mathematical standards for operator precedence, groupings, and so on for both integers and floating-point numbers. For instance, performing simple mathematics in PHP can be accomplished as follows (see Listing 1.8):

NOTE

For performing mathematical calculations, operator precedence refers to the order in which each mathematical operation is executed. For a detailed listing of operator precedence in PHP, consult the PHP manual online at http://www.php.net/manual/en/language.operators.php.


Listing 1.8. Simple Mathematics with PHP Variables
<?php

    $answer = 5 + 4;             /* $answer now equals 9 */
    $answer = $answer - 5;       /* $answer now equals 4 */
    $answer = $answer / 2;       /* $answer now equals 2 */
    $answer = 1/3;               /* $answer is now 0.333333 */
    $answer = ((5 + 4)*2) % 7;   /* $answer now equals 4 */

?>

NOTE

The preceding example uses the modulus operator %. This operator is used to determine the remainder in a whole-number division. In this case, you are determining the remainder when the whole number 18 is divided by the whole number 7. Because 7x2=14, the modulus is 1814=4.


An important consideration that must be made when dealing with floating point numbers is how they are handled when PHP converts them to an integer value. For instance, a value of 0.999999, when converted to an integer, may translate to 0, whereas on other systems it may translate to 1 as you would expect. This difference in behavior is the result of the system that PHP is running on, not PHP itself. For more information on this subject, including a description of how your particular system is affected, consult the PHP documentation.

As with most other C-style programming languages, PHP also supports a form of shorthand. In the preceding example, consider the second and third lines in which the result of a mathematical operation was stored in the same variable the operation was performed on. Instead of using the preceding syntax, you can save time by placing the desired operation next to the equal sign in the following fashion, as shown in Listing 1.9:

Listing 1.9. Shorthand Mathematics in PHP
<?php
    $answer = 5;         /* Assign Original value */
    $answer += 2;        /* Equivalent to:  $answer = $answer + 2; */
    $answer *= 2;        /* Answer is now 14 */
    $answer %= 5;        /* Answer is now 4 */
?>

To make your life even simpler, you can increment or decrement a variable by 1 by following the example shown in Listing 1.10:

Listing 1.10. Shorthand Incrementing/Decrementing of Variables
<?php

    $answer++;  /* Increment $answer by 1 */
    $answer--;  /* Decrement $answer by 1 */
    ++$answer;  /* Increment by 1, (see note) */

?>

NOTE

Although ++$answer and $answer++ are both perfectly valid PHP statements that increment the variable $answer by one, they are not completely the same! $answer++ increments the variable $answer after the statement's execution, whereas ++$answer increments the variable before execution. This is a critical difference in situations such as the following:

<?php

    $answer = 5;
    echo (++$answer)." ";
    echo "$answer<BR>";

    $answer = 5;
    echo ($answer++)." ":
    echo $answer;

?>

Which will output the following:

6 6 5 6


Along with simple mathematics, PHP also supports trigonometric and logarithmic operations for advanced calculations in the following fashion:

<?php
    $cos = cos(2 * M_PI);    /* cos of 2*PI is 1 */
?>

NOTE

M_PI is a predefined mathematical constant in PHP. For a complete listing of all defined mathematical (and other) constants available, consult the PHP Manual at http://www.php.net/math.


As stated earlier, the majority of string-manipulation techniques available to PHP are discussed later in this chapter. However, one string-specific operation does exist in PHP that should be discussed nowthe string concatenation operator. This operator is represented by a period (.) character and is used to combine two separate variables (usually strings) into a single string, as shown next:

<?php
    $string = "Thank you for buying ";
    $newstring = $string . "my book!";
?>

$newstring now contains the string "Thank you for buying my book!". To save time, this operator can also be used in its shorthand form like the mathematical operators described earlier.