Table Of ContentCore PHP Programming
Core PHP Programming Using PHP to Build
Dynamic Web Sites
Leon Atkinson
Publisher: Prentice Hall PTR
Second Edition August 03, 2000
ISBN: 0-13-089398-6, 800 pages
Master PHP 4 — the open source Web scripting breakthrough!
Contains expert coverage of syntax, functions, design, and
debugging!
Leverage the amazing performance of the new Zend engine!
650+ real-world code examples!
CD-ROM includes source code, plus everything you’ll need to
run PHP 4 implementations on Windows and UNIX!
This is the experienced developer’s guide to PHP. Master PHP
4, the open source, high-performance, cross-platform solution
for server-side scripting!
Core PHP Programming, Second Edition is the #1 practical
guide to PHP 4 for Web developers. With the guidance of top
PHP developer Leon Atkinson, you’ll learn everything you’ll
need to build robust, fast Web applications — and deploy them
on leading Web servers, from Apache to Microsoft Internet
Information Server.
Atkinson covers PHP syntax, the key building blocks of PHP
scripts, and every PHP function, including I/O, data, and math
functions, time, date, configuration, database, graphics, and
network functions. He presents PHP at work in sample code
that demonstrates sorting, searching, parsing, string evaluation,
and more. You’ll even find detailed, real-world insights into
PHP 4 program design and debugging!
Core PHP Programming delivers:
IT-SC book 1
Core PHP Programming
Thorough, easy-to-understand coverage of PHP syntax and
functions
Step-by-step guidance for PHP database integration
Design and optimization techniques for maximum
performance and extensibility
Practical debugging solutions
Companion CD-ROM includes PHP 4 source code and
Windows binaries plus all the code examples from the book!
IT-SC book 2
Core PHP Programming
CORE PHP Programming Using PHP to Build Dynamic Web Sites
Library of Congress Cataloging-in-Publication Date Atkinson, Leon.
Core PHP programming : using PHP to build dynamic Web sites / Leon Atkinson.--2nd
ed.
p. cm.
Includes bibliographical references and index.
1. PHP (Computer program language) 2. Web sites--Design. I. Title.
QA76.73.P22A85 2000
005.2'762--dc21
00-034019
IT-SC book 3
Core PHP Programming
Credits
Editorial/Production Supervision:
Jan H. Schwartz
Acquisitions Editor:
Mark Taub
Editorial Assistant:
Sarah Hand
Marketing Manager:
Kate Hargett
Manufacturing Manager:
Alexis Heydt
Cover Design:
Talar Agasyan
Cover Design Director:
Jerry Votta
Art Director:
Gail Cocker-Bogusz
Series Interior Design:
Meg VanArsdale
IT-SC book 4
Core PHP Programming
© 2001 Prentice Hall PTR
All rights reserved. No part of this book may be reproduced, in any form or by any
means, without permission in writing from the publisher.
All product names mentioned herein are the trademarks or registered trademarks of their
respective owners.
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
Prentice-Hall International (UK) Limited, London
Prentice-Hall of Australia Pty. Limited, Sydney
Prentice-Hall Canada Inc., Toronto
Prentice-Hall Hispanoamericana, S.A., Mexico
Prentice-Hall of India Private Limited, New Delhi
Prentice-Hall of Japan, Inc., Tokyo
Pearson Education Asia Pte. Ltd.
Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro
IT-SC book 5
Core PHP Programming
Foreword....................................................................................................................16
Preface........................................................................................................................18
Acknowledgments..................................................................................................20
Part I: PROGRAMMING WITH PHP...................................................................21
Chapter 1. AN INTRODUCTION TO PHP........................................................22
The Origins of PHP.............................................................................................22
What Makes PHP Better than Its Alternatives........................................24
Interfaces to External Systems....................................................................25
How PHP Works with the Web Server........................................................26
Hardware and Software Requirements......................................................26
Installation on Apache for UNIX...................................................................27
Installation on IIS for Windows NT.............................................................29
Editing Scripts......................................................................................................30
Algorithms.............................................................................................................30
What a PHP Script Looks Like.......................................................................31
Saving Data for Later.......................................................................................33
Receiving User Input.........................................................................................35
Choosing between Alternatives....................................................................38
Repeating Code...................................................................................................39
Conclusion.............................................................................................................40
Chapter 2. VARIABLES, OPERATORS, AND EXPRESSIONS...................41
Identifiers..............................................................................................................41
Data Types............................................................................................................41
Variable Creation and Scope..........................................................................45
Assigning Values to Variables........................................................................49
Retrieving Values...............................................................................................51
Freeing Memory..................................................................................................52
Constants...............................................................................................................52
Operators...............................................................................................................53
Logical and Relational Operators.................................................................54
Bitwise Operators...............................................................................................55
Miscellaneous Operators..................................................................................56
Assignment Operators......................................................................................59
Expressions...........................................................................................................60
Chapter 3. CONTROL STATEMENTS................................................................63
True and False.....................................................................................................63
The if Statement..............................................................................................63
The ? Operator....................................................................................................64
The switch Statement.....................................................................................68
Loops.......................................................................................................................70
The while Statement.......................................................................................70
The break Statement.......................................................................................73
IT-SC book 6
Core PHP Programming
The continue Statement................................................................................73
The do...while Statement...........................................................................76
The for Statement............................................................................................76
The foreach Statement..................................................................................79
exit, die, and return.....................................................................................79
Evaluation of Boolean Expressions..............................................................80
Chapter 4. FUNCTIONS........................................................................................81
Declaring a Function.........................................................................................81
The return Statement.....................................................................................82
Scope and the global Statement...............................................................83
Arguments.............................................................................................................83
Recursion...............................................................................................................86
Dynamic Function Calls....................................................................................89
Chapter 5. ARRAYS................................................................................................90
Single-Dimensional Arrays.............................................................................90
Indexing Arrays...................................................................................................91
Initializing Arrays...............................................................................................93
Multidimensional Arrays..................................................................................94
Casting Arrays.....................................................................................................95
Referencing Arrays Inside Strings...............................................................98
Chapter 6. CLASSES AND OBJECTS.............................................................100
Defining a Class................................................................................................101
Creating an Object...........................................................................................105
Accessing Properties and Methods............................................................105
Chapter 7. I/O AND DISK ACCESS................................................................109
HTTP Connections............................................................................................109
Writing to the Browser...................................................................................111
Output Buffering...............................................................................................112
Environment Variables...................................................................................112
Getting Input from Forms.............................................................................113
Cookies.................................................................................................................115
File Uploads........................................................................................................115
PUT Method Requests.....................................................................................117
Reading and Writing to Files........................................................................117
Sessions...............................................................................................................119
The include and require Functions.......................................................121
Part II: FUNCTIONAL REFERENCE.................................................................123
Chapter 8. I/O FUNCTIONS..............................................................................123
Sending Text to the Browser.......................................................................124
Output Buffering...............................................................................................126
Files........................................................................................................................127
Compressed File Functions...........................................................................163
POSIX....................................................................................................................168
IT-SC book 7
Core PHP Programming
Debugging...........................................................................................................173
Session Handling..............................................................................................195
Shell Commands...............................................................................................198
HTTP Headers....................................................................................................200
Network I/O........................................................................................................202
FTP.........................................................................................................................207
Chapter 9. DATA FUNCTIONS.........................................................................215
Data Types, Constants, and Variables.....................................................215
Arrays....................................................................................................................221
Hashing................................................................................................................243
Strings..................................................................................................................247
Encoding and Decoding.................................................................................255
Encryption...........................................................................................................277
Regular Expressions........................................................................................282
Perl-Compatible Regular Expressions......................................................285
Chapter 10. MATHEMATICAL FUNCTIONS..................................................291
Common Math...................................................................................................291
Random Numbers............................................................................................299
Arbitrary-Precision Numbers........................................................................302
Chapter 11. TIME, DATE, AND CONFIGURATION FUNCTIONS..........305
Time and Date...................................................................................................305
Alternative Calendars.....................................................................................312
Configuration.....................................................................................................316
Chapter 12. IMAGE FUNCTIONS....................................................................323
Analyzing Images.............................................................................................323
Creating JPEG, PNG, and WBMP Images................................................325
Chapter 13. DATABASE FUNCTIONS............................................................356
dBase....................................................................................................................356
DBM-style Database Abstraction................................................................361
filePro....................................................................................................................366
Informix...............................................................................................................368
InterBase.............................................................................................................375
mSQL.....................................................................................................................380
ODBC.....................................................................................................................406
Oracle....................................................................................................................417
Postgres...............................................................................................................436
Sybase..................................................................................................................445
Chapter 14. MISCELLANEOUS FUNCTIONS...............................................454
Apache..................................................................................................................454
Aspell.....................................................................................................................459
COM.......................................................................................................................460
Gettext..................................................................................................................462
IMAP......................................................................................................................463
Java........................................................................................................................485
IT-SC book 8
Core PHP Programming
LDAP......................................................................................................................486
Semaphores.......................................................................................................498
Shared Memory.................................................................................................500
SNMP.....................................................................................................................504
WDDX....................................................................................................................507
XML........................................................................................................................510
Part III: ALGORITHMS.......................................................................................520
Chapter 15. SORTING, SEARCHING, AND RANDOM NUMBERS........521
Sorting..................................................................................................................521
Bubble Sort.........................................................................................................522
Quicksort .............................................................................................................526
Built-In Sorting Functions.............................................................................528
Sorting with a Comparison Function.........................................................537
Searching.............................................................................................................540
Indexing...............................................................................................................540
Random Numbers............................................................................................547
Random Identifiers..........................................................................................548
Choosing Banner Ads......................................................................................549
Chapter 16. PARSING AND STRING EVALUATION..................................552
Tokenizing...........................................................................................................552
Regular Expressions........................................................................................555
Defining Regular Expressions......................................................................555
Using Regular Expressions in PHP Scripts..............................................557
Chapter 17. DATABASE INTEGRATION........................................................568
Building HTML Tables from SQL Queries.................................................568
Tracking Visitors with Session Identifiers...............................................575
Storing Content in a Database....................................................................579
Database Abstraction Layers.......................................................................583
Chapter 18. NETWORK.......................................................................................585
HTTP Authentication........................................................................................585
Controlling Browser Cache...........................................................................587
Setting Document Type.................................................................................589
Email with Attachments.................................................................................590
Verifying an Email Address...........................................................................594
Chapter 19. GENERATING GRAPHICS..........................................................600
Dynamic Buttons..............................................................................................600
Generating Graphs on the Fly.....................................................................605
Bar Graphs..........................................................................................................606
Pie Charts............................................................................................................609
Stretching Single-Pixel Images...................................................................612
Part IV: SOFTWARE ENGINEERING..............................................................614
Chapter 20. INTEGRATION WITH HTML......................................................615
Sprinkling PHP within an HTML Document.............................................615
Using PHP to Output All HTML.....................................................................619
IT-SC book 9