Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

PHP 5 For Dummies
Nội dung xem thử
Mô tả chi tiết
by Janet Valade
PHP 5
FOR
DUMmIES‰
01 541668 FM.qxd 3/25/04 2:50 PM Page i
01 541668 FM.qxd 3/25/04 2:51 PM Page vi
by Janet Valade
PHP 5
FOR
DUMmIES‰
01 541668 FM.qxd 3/25/04 2:50 PM Page i
PHP 5 For Dummies®
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030
www.wiley.com
Copyright © 2004 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or
by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing,
Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, e-mail:
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the
Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade
dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United
States and other countries, and may not be used without written permission. All other trademarks are the
property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor
mentioned in this book.
THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT
TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL
MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY
SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT
ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON
SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES
ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS
WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN
THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR
WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE
AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED
BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services or to obtain technical support, please contact
our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax
317-572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may
not be available in electronic books.
Library of Congress Control Number: 2003105680
ISBN: 0-7645-4166-8
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1B/RQ/QZ/QT/IN
01 541668 FM.qxd 3/25/04 2:50 PM Page ii
About the Author
Janet Valade is the author of PHP & MySQL For Dummies. In addition, she has
authored and revised chapters for several Linux books and for a Webmaster
certification book.
Janet Valade has 20 years experience in the computing field. She worked as a
Web designer/programmer for an engineering firm. Prior to that, Janet worked
for several years in a university environment as a systems analyst. During her
tenure, she supervised the installation and operation of computing resources,
designed and developed a data archive, provided technical support for faculty
and students, wrote numerous technical papers, and developed and presented seminars and workshops on a variety of technology topics.
Dedication
This book is dedicated to anyone who finds it useful.
Acknowledgments
I wish to express my appreciation to the entire Open Source community.
Without those who give their time and talent, there would be no cool PHP for
me to write about. Furthermore, I never would have learned this software
without the PHP lists where people generously spend their time answering
foolish questions from beginners. Many ideas have come from reading questions and answers on the lists.
01 541668 FM.qxd 3/25/04 2:50 PM Page iii
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments through our online registration form
located at www.dummies.com/register/.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and
Media Development
Project Editors: Kala Schrager,
Christopher Morris
Acquisitions Editor: Terri Varveris
Senior Copy Editors: Kim Darosett,
Teresa Artman
Technical Editor: Szemir Khangyi
Editorial Manager: Kevin Kirschner
Media Development Manager:
Laura VanWinkle
Media Development Supervisor:
Richard Graves
Editorial Assistant: Amanda Foxworth
Cartoons: Rich Tennant (www.the5thwave.com)
Production
Project Coordinator: Maridee Ennis
Layout and Graphics: Andrea Dahl,
Joyce Haughey, Stephanie D. Jumper,
Michael Kruzil, Heather Ryan,
Jacque Schneider
Proofreaders: Carl Pierce, TECHBOOKS
Production Services
Indexer: TECHBOOKS Production Services
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Joyce Pepple, Acquisitions Director
Composition Services
Gerry Fahey, Vice President of Production Services
Debbie Stailey, Director of Composition Services
01 541668 FM.qxd 3/25/04 2:51 PM Page iv
Contents at a Glance
Introduction ................................................................1
Part I: Say Hello to the PHP Scripting Language ...........7
Chapter 1: Getting to Know PHP ......................................................................................9
Chapter 2: Setting Up the Environment ........................................................................19
Chapter 3: Creating Your First PHP Script ....................................................................35
Part II: Variables and Data ........................................51
Chapter 4: Using Variables in PHP Scripts ...................................................................53
Chapter 5: Working with Data ........................................................................................73
Chapter 6: Storing Data in Groups by Using Arrays ....................................................97
Part III: Basic PHP Programming .............................127
Chapter 7: Controlling the Flow of the Script ............................................................129
Chapter 8: Reusing PHP Code ......................................................................................157
Chapter 9: Object-Oriented Programming Meets PHP ..............................................177
Part IV: Common PHP Applications ...........................197
Chapter 10: The Basics of Web Applications .............................................................199
Chapter 11: Other Web Applications ..........................................................................223
Chapter 12: Storing Data with PHP ..............................................................................247
Chapter 13: PHP and Your Operating System ............................................................279
Chapter 14: PHP Extensions .........................................................................................303
Part V: The Part of Tens ...........................................319
Chapter 15: Ten Things to Look For When Troubleshooting a Script ....................321
Chapter 16: Ten PHP Resources You Can’t Live Without .........................................327
Part VI: Appendixes .................................................331
Appendix A: Installing PHP............................................................................................333
Appendix B: Useful PHP Built-in Functions.................................................................353
Index .......................................................................377
01 541668 FM.qxd 3/25/04 2:51 PM Page v
01 541668 FM.qxd 3/25/04 2:51 PM Page vi
Table of Contents
Introduction .................................................................1
About This Book ..............................................................................................1
How to Use This Book ....................................................................................2
Foolish Assumptions ......................................................................................3
How This Book Is Organized ..........................................................................4
Part I: Say Hello to the PHP Scripting Language ................................4
Part II: Variables and Data ....................................................................4
Part III: Basic PHP Programming .........................................................4
Part IV: Common PHP Applications ....................................................4
Part V: The Part of Tens ........................................................................5
Part VI: Appendixes ...............................................................................5
Icons Used in This Book .................................................................................5
Part I: Say Hello to the PHP Scripting Language ............7
Chapter 1: Getting to Know PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Getting Familiar with PHP ..............................................................................9
Considering the Various Uses for PHP .......................................................10
Using PHP for Web applications ........................................................11
Using PHP for database applications ................................................12
Using PHP with your file system .......................................................13
Using PHP for system commands .....................................................13
Understanding How PHP Works ..................................................................14
PHP as a general-purpose language ..................................................14
PHP for the Web ...................................................................................15
Keeping Up with Changes in PHP ................................................................16
PHP 5 .....................................................................................................17
Previous versions of PHP ...................................................................18
Chapter 2: Setting Up the Environment . . . . . . . . . . . . . . . . . . . . . . . . . .19
Establishing Your Web Environment ..........................................................19
Using an existing Web environment ..................................................21
Choosing a Web hosting company ....................................................22
Setting up your own Web environment ............................................25
Testing PHP ..........................................................................................27
Setting Up PHP for General-Purpose Scripting ..........................................30
Configuring PHP ............................................................................................32
Using Tools to Build PHP Scripts ................................................................32
Programming editors ..........................................................................32
Integrated Development Environment (IDE) ...................................33
01 541668 FM.qxd 3/25/04 2:51 PM Page vii
Chapter 3: Creating Your First PHP Script . . . . . . . . . . . . . . . . . . . . . . .35
Writing PHP Statements ...............................................................................36
Building Scripts .............................................................................................37
Adding PHP statements to HTML pages ...........................................38
Using PHP independent of the Web ..................................................40
Writing Your First Script ...............................................................................42
Discovering More about Output Statements .............................................44
Processing PHP output statements ...................................................45
Using special characters in output statements ...............................46
Documenting the Script ................................................................................48
Part II: Variables and Data .........................................51
Chapter 4: Using Variables in PHP Scripts . . . . . . . . . . . . . . . . . . . . . .53
Naming Variables ...........................................................................................53
Assigning and Displaying Variable Values ..................................................55
Creating variables ................................................................................55
Displaying variable values .................................................................56
Writing Your First Script That Uses Variables ...........................................57
Discovering More about Output Statements with Variables ...................59
Using Variable Variables ...............................................................................60
Removing Variables ......................................................................................61
Working with Constants ...............................................................................62
Creating constants ..............................................................................62
Understanding when to use constants .............................................63
Displaying constants ...........................................................................66
Utilizing built-in PHP constants .........................................................66
Handling Error Messages .............................................................................67
Changing the error level for your Web site ......................................68
Changing the error level for a script .................................................69
Sending messages to a log ..................................................................70
Advanced error handling ....................................................................70
Chapter 5: Working with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
Understanding Data Types ...........................................................................73
Assigning data types ...........................................................................74
Type casting .........................................................................................74
Working with Numbers .................................................................................75
Performing mathematical operations ...............................................75
Formatting numbers for output .........................................................79
Working with Character Strings ..................................................................81
Using special characters in strings ...................................................82
Comparing single-quoted strings and double-quoted strings .......82
Escaping characters ............................................................................84
Joining strings together ......................................................................85
Manipulating strings ...........................................................................85
Formatting output strings ..................................................................89
viii PHP 5 For Dummies
01 541668 FM.qxd 3/25/04 2:51 PM Page viii
Working with Dates and Times ....................................................................92
Formatting dates .................................................................................92
Storing a timestamp in a variable ......................................................94
Chapter 6: Storing Data in Groups by Using Arrays . . . . . . . . . . . . . . .97
Creating and Working with Arrays ..............................................................97
Creating arrays ....................................................................................97
Viewing arrays ...................................................................................100
Modifying arrays ................................................................................101
Removing values from arrays ..........................................................102
Sorting Arrays ..............................................................................................103
Using Arrays in Statements ........................................................................105
Using arrays in echo statements .....................................................106
Using arrays in list statements ........................................................106
Walking through an Array ..........................................................................107
Traversing an array manually ..........................................................108
Using foreach to walk through an array .........................................109
Finding Array Size .......................................................................................110
Converting Arrays into Strings (And Vice Versa) ...................................110
Converting Variables into Arrays (And Vice Versa) ................................112
Splitting and Merging Arrays .....................................................................113
Comparing Arrays .......................................................................................115
Working with Other Array Operations .....................................................116
Summing arrays .................................................................................116
Removing duplicate items ................................................................117
Exchanging keys and values ............................................................117
Multidimensional Arrays ............................................................................118
Creating multidimensional arrays ...................................................119
Viewing multidimensional arrays ....................................................120
Using multidimensional arrays in statements ...............................120
Walking through a multidimensional array ....................................121
Built-in PHP Arrays .....................................................................................123
Using superglobal arrays ..................................................................123
Using $_SERVER and $_ENV .............................................................125
Using $argv and $argc .......................................................................125
Part III: Basic PHP Programming ..............................127
Chapter 7: Controlling the Flow of the Script . . . . . . . . . . . . . . . . . . .129
Changing the Order of Statement Execution ...........................................129
Setting Up Conditions .................................................................................131
Using comparison operators ...........................................................131
Checking variable content ...............................................................133
Pattern matching with regular expressions ...................................133
Joining multiple comparisons ..........................................................138
Table of Contents ix
01 541668 FM.qxd 3/25/04 2:51 PM Page ix
Using Conditional Statements ...................................................................140
Using if statements ............................................................................140
Using switch statements ..................................................................144
Repeating Actions by Using Loops ...........................................................145
Using for loops ...................................................................................146
Using while loops ..............................................................................149
Using do..while loops ........................................................................151
Avoiding infinite loops ......................................................................153
Breaking out of a loop .......................................................................154
Chapter 8: Reusing PHP Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .157
Inserting Code in Your Script ....................................................................158
Including files .....................................................................................158
Storing include files ...........................................................................160
Setting up include directories ..........................................................161
Creating Reusable Code (Functions) ........................................................162
Defining functions .............................................................................163
Using variables in functions .............................................................165
Passing values to a function ............................................................167
Returning a value from a function ...................................................171
Using built-in functions .....................................................................174
Handling Errors ..........................................................................................174
Chapter 9: Object-Oriented Programming Meets PHP . . . . . . . . . . .177
Introducing Object-Oriented Programming .............................................177
Objects and classes ...........................................................................178
Properties ...........................................................................................179
Methods ..............................................................................................179
Inheritance .........................................................................................180
Object-oriented concepts PHP 5 omits ...........................................180
Developing an Object-Oriented Program .................................................181
Choosing objects ...............................................................................181
Selecting properties and methods for each object .......................182
Creating and using the class ............................................................182
Defining a Class ...........................................................................................183
Writing a class statement .................................................................183
Setting properties ..............................................................................184
Using $this ..........................................................................................185
Adding methods ................................................................................185
Writing the constructor ....................................................................186
Putting it all together ........................................................................187
Using a Class ................................................................................................190
Making Properties and Methods Private ..................................................191
Using Exceptions .........................................................................................194
Copying Objects ..........................................................................................195
Destroying Objects .....................................................................................196
x PHP 5 For Dummies
01 541668 FM.qxd 3/25/04 2:51 PM Page x
Part IV: Common PHP Applications ...........................197
Chapter 10: The Basics of Web Applications . . . . . . . . . . . . . . . . . . .199
Securing Your Web Site ...............................................................................200
Ensuring the security of the host computer ..................................200
Keeping information private ............................................................201
Being cautious of information from users ......................................202
Using a secure Web server ...............................................................202
Displaying Static Web Pages ......................................................................203
Working with HTML Forms ........................................................................203
Collecting information from Web site visitors ...............................204
Receiving the information ...............................................................211
Checking the information .................................................................213
Cleaning information .........................................................................220
Chapter 11: Other Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . .223
Overcoming Statelessness .........................................................................223
Navigating Web Sites with Multiple Pages ...............................................224
Echoing links ......................................................................................224
Using forms ........................................................................................224
Relocating users ................................................................................225
Moving Information from Page to Page ....................................................226
Adding information to the URL ........................................................227
Passing information via cookies ......................................................229
Passing information using HTML forms .........................................231
Using PHP sessions ...........................................................................232
Uploading Files ............................................................................................238
Using a form to upload a file ............................................................239
Accessing information about an uploaded file ..............................240
Moving uploaded files to their destination ....................................241
Putting it all together ........................................................................241
Using JavaScript with PHP .........................................................................244
Adding JavaScript code to a PHP script .........................................244
Using PHP variables with JavaScript ..............................................245
Chapter 12: Storing Data with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . .247
Using Flat Files .............................................................................................249
Accessing files ...................................................................................249
Writing to a file ...................................................................................252
Reading from a file .............................................................................252
Exchanging data with other programs ...........................................255
Working with Databases .............................................................................259
Understanding database software ..................................................259
Understanding database support in PHP .......................................262
Communicating with your database ...............................................265
xi Table of Contents
01 541668 FM.qxd 3/25/04 2:51 PM Page xi
Using PHP with a database ...............................................................266
Handling errors ..................................................................................272
Putting it all together ........................................................................273
Using SQLite .................................................................................................276
Chapter 13: PHP and Your Operating System . . . . . . . . . . . . . . . . . . .279
Managing Files .............................................................................................279
Getting information about files ........................................................280
Copying, renaming, and deleting files .............................................282
Organizing files ..................................................................................283
Using Operating System Commands ........................................................286
Using backticks ..................................................................................288
Using the system function ................................................................289
Using the exec function ....................................................................289
Using the passthru function .............................................................290
Understanding security issues ........................................................291
Using FTP .....................................................................................................292
Logging in to the FTP server ............................................................292
Getting a directory listing ................................................................293
Downloading and uploading files with FTP ....................................294
Other FTP functions ..........................................................................295
Using E-Mail .................................................................................................297
Setting up PHP to send e-mail ..........................................................297
Sending e-mail messages ..................................................................299
Sending e-mail attachments .............................................................300
Chapter 14: PHP Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303
Investigating the Basic Extensions ...........................................................303
Taking a Look at Standard Extensions ......................................................305
Using PEAR ...................................................................................................309
Finding a PEAR Package ...................................................................309
Setting up PEAR .................................................................................311
Installing a PEAR package ................................................................313
Using a PEAR package .......................................................................314
Part V: The Part of Tens ............................................319
Chapter 15: Ten Things to Look For When
Troubleshooting a Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321
Missing Semicolons .....................................................................................321
Not Enough Equal Signs .............................................................................322
Misspelled Variable Names ........................................................................322
Missing Dollar Signs ....................................................................................323
Troubling Quotes ........................................................................................323
Invisible Output ...........................................................................................324
Numbered Arrays ........................................................................................324
Including PHP Statements ..........................................................................325
xii PHP 5 For Dummies
01 541668 FM.qxd 3/25/04 2:51 PM Page xii