LINK DOWNLOAD MIỄN PHÍ TÀI LIỆU "Tài liệu PHP in a Nutshell doc": http://123doc.vn/document/1038009-tai-lieu-php-in-a-nutshell-doc.htm
IN A NUTSHELL
Paul Hudson
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
PHP in a Nutshell
by Paul Hudson
Copyright © 2006 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online
editions are also available for most titles (safari.oreilly.com). For more information, contact
our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editors:
Allison Randal
Tatiana Apandi
Production Editor:
Adam Witwer
Cover Designer:
Karen Montgomery
Interior Designer:
David Futato
Printing History:
October 2005: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered
trademarks of O’Reilly Media, Inc. PHP in a Nutshell,the image of a cuckoo, and related trade
dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and O’Reilly Media,
Inc. was aware of a trademark claim, the designations have been printed in caps or initial
caps.
While every precaution has been taken in the preparation of this book, the publisher and
author assume no responsibility for errors or omissions, or for damages resulting from the use
of the information contained herein.
This book uses RepKover
™
, a durable and flexible lay-flat binding.
ISBN-10: 0-596-10067-1
ISBN-13: 978-0-596-10067-4
[M] [02/07]
v
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Chapter 1
Table of Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xi
1. Introduction to PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
PHP History 1
Advantages of PHP 2
Getting Help 4
Getting Certified 7
PHP Resources 7
2. Installing PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
Installing on Windows 11
Installing on Unix 14
Testing Your Configuration 17
System Configuration 17
3. The PHP Interpreter
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
Running PHP Scripts 19
Extending PHP 20
PEAR 21
Abnormal Script Termination 22
4. The PHP Language
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
The Basics of PHP 23
Variables 24
Whitespace 26
Heredoc 26
vi
|
Table of Contents
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Brief Introduction to Variable Types 27
Code Blocks 27
Opening and Closing Code Islands 27
Comments 28
Conditional Statements 28
Case Switching 30
Loops 31
Infinite Loops 34
Special Loop Keywords 34
Loops Within Loops 35
Mixed-Mode Processing 37
Including Other Files 37
Functions 39
5. Variables and Constants
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
Types of Data 47
True or False 48
Strings 48
Integers 49
Floats 50
Automatic Type Conversion 51
Checking Whether a Variable Is Set: isset( ) 52
Variable Scope 52
Variable Variables 53
Superglobals 54
Using $_ENV and $_SERVER 55
References 57
Constants 58
Arrays 60
6. Operators
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79
Arithmetic Operators 79
Assignment Operators 80
String Operators 81
Bitwise Operators 81
Comparison Operators 82
Incrementing and Decrementing Operators 83
Logical Operators 84
Some Operator Examples 85
The Ternary Operator 86
The Execution Operator 86
Operator Precedence and Associativity 87
Table of Contents | vii
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
7. Function Reference
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
89
Undocumented Functions 90
Handling Non-English Characters 90
8. Object-Oriented PHP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
128
Conceptual Overview 128
Classes 129
Objects 131
Properties 132
The ‘this’ Variable 133
Objects Within Objects 133
Access Control Modifiers 134
Object Type Information 140
Class Type Hints 141
Constructors and Destructors 142
Copying Objects 145
Comparing Objects with == and === 146
Saving Objects 147
Magic Methods 148
Static Class Methods and Properties 152
Helpful Utility Functions 153
Interfaces 153
Dereferencing Object Return Values 156
9. HTML Forms
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
157
What Does It Mean to Be Dynamic? 158
Designing a Form 158
Handling Data 162
Splitting Forms Across Pages 166
Validating Input 166
Form Design 169
Summary 169
10. Cookies and Sessions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
170
Cookies Versus Sessions 170
Using Cookies 171
Using Sessions 173
Storing Complex Data Types 179
11. Output Buffering
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
181
Why Use Output Buffering? 181
Getting Started 182
viii
|
Table of Contents
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Reusing Buffers 182
Stacking Buffers 182
Flushing Stacked Buffers 183
Reading Buffers 184
Other OB Functions 185
Flushing Output 185
Compressing Output 187
URL Rewriting 188
12. Security
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
190
Security Tips 190
Encryption 192
13. Files
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
195
Reading Files 195
Creating and Changing Files 199
Moving, Copying, and Deleting Files 200
Other File Functions 202
Checking Whether a File Exists 203
Retrieving File Time Information 203
Dissecting Filename Information 204
Handling File Uploads 205
Locking Files with flock( ) 206
Reading File Permissions and Status 208
Changing File Permissions and Ownership 209
Working with Links 210
Working with Directories 210
Remote Files 212
File Checksums 213
Parsing a Configuration File 213
14. Databases
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
216
Using MySQL with PHP 216
PEAR::DB 223
SQLite 228
Persistent Connections 232
MySQL Improved 233
15. Regular Expressions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
234
Basic Regexps with preg_match( ) and preg_match_all( ) 234
Regexp Character Classes 235
Regexp Special Characters 236
Table of Contents | ix
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Words and Whitespace Regexps 239
Storing Matched Strings 240
Regular Expression Replacements 240
Regular Expression Syntax Examples 242
The Regular Expressions Coach 243
16. Manipulating Images
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
244
Getting Started 244
Choosing a Format 246
Getting Arty 247
More Shapes 248
Complex Shapes 250
Outputting Text 251
Loading Existing Images 254
Color and Image Fills 255
Adding Transparency 257
Using Brushes 258
Basic Image Copying 260
Scaling and Rotating 262
Points and Lines 265
Special Effects Using imagefilter( ) 267
Interlacing an Image 269
Getting an Image’s MIME Type 269
17. Creating PDFs
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
271
Getting Started 271
Adding More Pages and More Style 273
Adding Images 274
PDF Special Effects 275
Adding Document Data 275
18. Creating Flash
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
277
A Simple Movie 277
Flash Text 279
Actions 280
Animation 282
19. XML & XSLT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
284
SimpleXML 284
Transforming XML Using XSLT 290
x
|
Table of Contents
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
20. Network Programming
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
293
Sockets 293
HTTP 297
Sending Mail 300
Curl 306
21. Distributing Your Code
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
313
Cross-Platform Code 1: Loading Extensions 313
Cross-Platform Code 2: Using Extensions 314
Cross-Platform Code 3: Path and Line Separators 314
Cross-Platform Code 4: Coping with php.ini Differences 315
Cross-Platform Code 5: Checking the PHP Version
with phpversion( ) and version_compare( ) 316
22. Debugging
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
317
The Most Basic Debugging Technique 317
Making Assertions 318
Triggering Your Own Errors 320
Testing with php_check_syntax( ) 321
Source Highlighting 321
Handling MySQL Errors 322
Exception Handling 323
Backtracing Your Code 325
Custom Error Handlers 327
Custom Exception Handlers 330
Using @ to Disable Errors 330
phpinfo( ) 331
Output Style 331
23. Performance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
335
Write Your Code Sensibly 335
Use the Zend Optimizer 336
Use a PHP Code Cache 336
Compress Your Output 336
Don’t Use CGI 337
Debug Your Code 337
Use Persistent Connections 337
Compile Right 337
Index
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
339
xi
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Chapter 2
Preface
Now installed on more than 20 million Internet domains around the world, PHP
is the undisputed king of web programming languages. Its users cite many reasons
for deployment, such as database connectivity, powerful extensions, and rich
object-orientation, but nearly everyone would agree that, above all, PHP is just
plain easy to use. This is the feature that continues to drive the language forward,
attracting new users and enabling existing programmers to do more with their
skills.
The release of PHP 5 has introduced many new features to the language, making
this an exciting time for the language. Many people who had not previously
considered PHP are now finding it a good fit for their needs—the new object-
orientation system is a big plus, for example. More importantly, many people who
had a large investment in PHP 4 are trying to migrate their code to the new release
with minimum breakage. Fortunately for all of us, it’s not too hard to retain back-
ward compatibility, and it’s very easy to take advantage of the many new features.
So, welcome to PHP. I think you’ll find it a fun, interesting, and flexible language
that might finally disprove the old saying, “Cheap, good, on time: choose any
two.”
Audience
This book has been designed to be of maximum use for existing PHP developers
looking for a complete, compact, and portable reference guide to the language. If
this is your first time using PHP, but you have experience using similar languages
such as Perl, Python, or C, then you should be able to treat the book as a learning
guide.
As any PHP programmer will tell you, the online PHP manual is of a very high
standard. The aim of this book is not to compete with or replace the online
manual. Although this book is designed to stand alone, you will find the topic
grouping, tips, and examples here complement the online guide.
xii
|
Preface
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Assumptions
This book assumes you are familiar with variables, loops, and other basic
programming concepts. Although this material is explained to a degree, it is
recommended that you at least have some experience using PHP or a similar
programming language.
Contents of This Book
Chapter 1, Introduction to PHP, covers the general characteristics of the PHP
language and its implementations, and discusses where to get help and
information.
Chapter 2, Installing PHP, explains how to obtain and install PHP.
Chapter 3, The PHP Interpreter, covers the PHP interpreter and its use for running
PHP programs on a web server or on the command line.
Chapter 4, The PHP Language, covers PHP syntax, variables, control structures,
includes, and user-defined functions.
Chapter 5, Variables and Constants, examines the different variable types in PHP.
Chapter 6, Operators, outlines the basic operators in PHP.
Chapter 7, Function Reference, is a reference chapter for the most commonly used
built-in functions of PHP.
Chapter 8, Object-Oriented PHP, explains object-oriented programming in PHP.
Chapter 9, HTML Forms, is an introduction to creating HTML forms and
processing them in PHP.
Chapter 10, Cookies and Sessions, explains the tools for using cookies and sessions
in PHP.
Chapter 11, Output Buffering, describes how to buffer your output, and when you
would want to do so.
Chapter 12, Security, covers a few essential security considerations when running
PHP websites.
Chapter 13, Files, is a reference to the functions for interacting with files.
Chapter 14, Databases, gives a brief introduction to accessing MySQL and SQLite
databases in PHP, and to
PEAR::DB, which provides a consistent interface to many
different database packages.
Chapter 15, Regular Expressions, covers some powerful ways to process strings,
including matching, extracting substrings, and replacing text.
Chapter 16, Manipulating Images, shows how to create and alter images with the
GD library that ships with PHP.
Chapter 17, Creating PDFs, shows how to create PDFs in PHP using PDFlib.
Không có nhận xét nào:
Đăng nhận xét