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

References

Variable References

To wrap up our discussion of PHP programming fundamentals, we'll look at creating variable references. The concept of references in PHP is essentially to allow the developer to reference the data contained with a variable by one or more variable names. This means more than both variables having the same value (for example, $a and $b both equaling 5). When one variable is referenced to another, any change made to either variable will also change the other variable it is referenced to.

In PHP, references are created by prefixing a variable (or function) name by an ampersand (&) character. Consider the example in Listing 1.27:

Listing 1.27. Using PHP References
<?php

    $myvar = 42;       /* Initialize $myvar */
    $myref = &$myvar;  /* Create a reference $myref to $myvar */

    echo "The value of \$myref is '$myref'<BR>";
    echo "The value of \$myvar is '$myvar'<BR>";

    $myvar++;

    echo "The value of \$myref is '$myref'<BR>";
    echo "The value of \$myvar is '$myvar'<BR>";

    $myref--;

    echo "The value of \$myref is '$myref'<BR>";
    echo "The value of \$myvar is '$myvar'<BR>";

?>

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

The value of $myref is '42'
The value of $myvar is '42'
The value of $myref is '43'
The value of $myvar is '43'
The value of $myref is '42'
The value of $myvar is '42'

As you can see, the variables $myvar and $myref are now references/aliases to the same piece of data, and any change made to one affects the other.

NOTE

Because both $myvar and $myref represent the same data, if you destroy either variable using the unset() PHP function, the data would not be lost. Rather, the remaining variable would still point to the data. This will be true regardless of how many different references to a single variable are destroyed. As long as one variable still references a given piece of data, it can be accessed from within your scripts through that variable.


References Used in Functions

References can also be used in conjunction with functions. For instance, consider a situation in which it would be beneficial to return more than one value from a function. It is impossible to return two values using the return statement, and it may not be desirable to set global variables. By using references you can return as many values as desired in a relatively clean fashion.

To define a parameter to a function as a reference, prefix the parameter variable name with the reference operator & and pass a reference to the function when called, as shown in Listing 1.28:

Listing 1.28. Passing by Reference in PHP
<?php

    function reference_test($var, &$result, &$result2) {

        $result = "This is return value #1";
        $result2 = "You entered $var as your parameter";

        return 42;

    }

    $res = reference_test(10, &$res1, &$res2);

    echo "The value of \$res is '$res'<BR>";
    echo "The value of \$res1 is '$res1'<BR>";
    echo "The value of \$res2 is '$res2'<BR>";

?>

This produces the following output:

The value of $res is '42'
The value of $res1 is 'This is return value #1'
The value of $res2 is 'You entered 10 as your parameter'

If you are confused as to how this script works, let's walk through it step by step. First, our function reference_test() is declared accepting three parameters. The first parameter, $val, is a standard PHP parameter, whereas the remaining two, $result and $result2, are reference parameters. When the reference_test() function is called, it is passed three parameters. The first is a constant value of 10, and the remaining two are references to the variables $res1 and $res2. When this function call is executed, a link between the variables $result and $result2 and the variables $res1 and $res2 is created (because they reference each other). Hence, when changes are made to the $result and $result2 variables from within the function, the linked references outside the function $res1 and $res2 are also changed. The function still returns an integer constant of 42, which is then stored in the $res variable as expected.

NOTE

Don't worry if this script confuses you at first. References are one of the hardest topics in PHP to completely understand (especially as things get more complicated) and it will take some practice to get the hang of them.

Also note that you don't need to pass by reference at runtime and in the function declaration. Either place will have the same result. The only difference is that declaring in the function definition means all calls to the function are automatically passed by reference.


Along with passing variables to functions by reference, PHP supports the returning of variable references. In the example shown in Listing 1.29, this concept is used to create a function that returns a reference based on the values of the parameters passed.

Listing 1.29. Returning a Value by Reference
<?php

    function &find_var($one, $two, $three) {

        if(($one > 0) && ($one <= 10)) return $one;
        if(($two > 0) && ($two <= 10)) return $two;
        if(($three > 0) && ($three <= 10)) return $three;

    }

    $c_one = 'foo';
    $c_two = 42;
    $c_three = 4;

    $right_var = &find_var($c_one, $c_two, $c_three);

    $right_var++;

    echo "The value of \$c_three and \$right_var are: ";
    echo "$c_three and $right_var<BR>\n";

?>

When this code is executed, the find_var() function will determine which of the three parameters is between 1 and 10 and return a reference to that variable, which will then be linked to the $right_var variable. The result is that when $right_var is incremented, the only variable that met the requirements (the $c_three variable) will also be incremented, resulting in the following output:

The value of $c_three and $right_var are: 5 and 5