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

Multiple File PHP Scripts

It is always good practice to make your scripts as modular as possible, designing your functions in such a way that they can be used in other PHP scripts. In this respect, and as you accumulate an ever-growing library of functions, the need to organize them becomes more and more paramount. In PHP, this organization is accomplished by separating your scripts into multiple files and including them when appropriate. Furthermore, by storing sensitive static information such as database login information in separate files, they can be safely placed outside the Web tree of the server and thus be inaccessible by the public.

Regardless of the reasons, inclusion of external files is accomplished through the include, include_once, require, and require_once PHP statements. As you may suspect, of these four statements only the include and require statements actually differ with any great significance, and it is those differences that I'll focus on. First, let's discuss how each of the two flavors work.

NOTE

The only difference between the include/require and include_once/require_once statements is how many times a given file will actually be loaded. When the include_once/require_once statements are used, the file cannot be loaded or executed multiple times. If an attempt is made to load a file twice using one of these two methods, it will be ignored. Because it is unacceptable to define the same function multiple times within a script, these functions allow the developer to include a script as needed without having to check whether it has been previously loaded.


The general syntax of both the include and require statements are as follows:

include "file_to_load.php";
include_once "file_to_load.php";

or

require "file_to_load.php";
require_once "file_to_load.php";

Note that for every file inclusion function previously listed, the file to load can be a string constant or a variable containing the name of the file.

NOTE

If URL wrappers are enabled in PHP (see Chapter 20, "Working with the File System," and Chapter 21, "Network I/O"), the filename provided to the include statement can be a HTTP address of the file to be loaded.


As I mentioned earlier, it is good practice to separate functions and code that are used in multiple scripts into a separate file. Following that logic, I'll assume that a PHP file exists called library.inc, which contains the is_leapyear() function defined in Listing 1.19. Note that although this file is designed to be "included" rather than directly executed by PHP, it still conforms to all the rules of a normal PHP script. This means that all PHP code must be in appropriate tags, and so on. Note that non-PHP files (such as HTML files) may also be included; they will be dumped into the output as you would expect.

Assuming that library.inc is in the same directory as the actual script, you could use your is_leapyear() function that exists within the library.inc file, as shown in Listing 1.24:

Listing 1.24. Using include to Load Files in PHP
<?php

    include ('library.inc');     // Parentheses are optional 
    $leap = is_leapyear(2003);

?>

NOTE

In most practical situations, files that are included into PHP scripts are not in the same directory as the script that actually requires them. Often, all the includable files are stored in a directory that is then designated part of the PHP include file search path. When a file is requested to be included by a PHP script, PHP first checks the current directory for the file followed by the include file path before returning an error.


Likewise, the require statement may also be used to include the file, as shown in Listing 1.25:

Listing 1.25. Using require to Load Files in PHP
<?php

    require ('library.inc');     // Parentheses are optional
    $leap = is_leapyear(2003);

?>

If both statements will allow the current script to execute the code in a separate file, what is the difference between the two? There are two major differences: the first is the capability to return values and the second is under what circumstances the requested file is loaded. When an include statement is used, PHP delays the actual loading of the requested file until the script reaches the point of executing the include statement and replaces the include statement with the contents of the file. Conversely, in the case of the require statement, the require statement is replaced with the contents of the requested file regardless of whether the require statement (and thus the contents of the file) would have executed in the normal progression of the script.

That is all fine, but what exactly does it mean to return a value from an external file? Consider the code shown in Listing 1.26, which we'll assume is stored in the file test.inc and its associated script includetest.php:

Listing 1.26. Behavior of Files Included Using include
<?php

    /* test.inc file */
    echo "Inside the included file<BR>";

    return "Returned String";

    echo "After the return inside the include<BR>";

?>

<?php

    /* includetest.php file */
    echo "Inside of includetest.php<BR>";

    $ret = include ('test.inc');

    echo "Done including test.inc<BR>";
    echo "Value returned was '$ret'";

?>

When includetest.php is executed, what is the result? In this case, the result would be the following:

Inside of includetest.php
Inside the included file
Done including test.inc
Value returned was 'Returned String'

As you can see, not only are external files useful for storing libraries of common PHP functions, they can actually be PHP "functions" when using the include statement. Note that when the return statement was executed from within your includetest.php file, the execution of the remainder of the file terminated.

NOTE

The capability to return values from external files is limited only to the include and include_once statements. The require and require_once statements cannot be used in this fashion.