php 5 for dummies

405 2.3K 0
php 5 for dummies

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 permit- ted 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: permcoordinator@wiley.com. 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 DIS- CLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PAR- TICULAR 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 PROFES- SIONAL 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 pre- sented 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 ques- tions 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 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 PHP 5 For Dummies viii 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 ix Table of Contents 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 PHP 5 For Dummies x 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 [...]... changed PHP for the Web is called php- cgi; PHP CLI is called just php, as in php. exe on Windows Both are stored in the directory where PHP is installed Prior to PHP 5, both programs were named php. exe, but stored in different subdirectories PHP 5 adds support for MySQL 4.1 and later However, support for MySQL is not included with PHP 5 by default Support for MySQL 4.0 or MySQL 4.1 must be specified when PHP. .. contains information you need to know So, right now, before you forget, hop over to the PHP Web site and sign up for a list or two at www .php. net/mailing-lists .php PHP 5 Most of the important changes in PHP version 5 don’t affect the coding or the use of PHP They affect the performance of PHP The Zend engine (the magic, invisible engine that powers PHP) has been significantly improved, and as a result,... The information here can be interesting and helpful, but you don’t need to understand it to use the information in the book 5 6 PHP 5 For Dummies Part I Say Hello to the PHP Scripting Language I In this part provide an overview of PHP I describe PHP, how it works, and what it is useful for After describing your tools, I show you how to set up your working environment I also present options for accessing... Functions 353 Array Functions 353 Date and Time Functions 358 File System Functions 359 HTTP and Mail Functions 366 Mathematical Functions .367 PHP Options and Information Functions 369 String Functions 370 Variable Functions 3 75 Index 377 xiii xiv PHP 5 For Dummies Introduction B ecause you’re... themselves PHP has many features designed specifically for use in Web sites, including the following: ߜ Interact with HTML forms: PHP can display an HTML form and process the information that the user types in ߜ Communicate with databases: PHP can interact with databases to store information from the user or retrieve information that is displayed to the user ߜ Generate secure Web pages: PHP allows... statements The PHP language statements are not included in the HTML sent to the browser, so the PHP code is secure and transparent to the user For example, consider this simple PHP statement: < ?php echo “Hello World”; ?> In this statement, < ?php is the PHP opening tag, ?> is the closing tag, and echo is a PHP instruction that tells PHP to output the text that follows it as plain HTML code The PHP software... Mates 3 25 Confusing Parentheses and Brackets .326 Chapter 16: Ten PHP Resources You Can’t Live Without 327 The PHP Web Site 327 PHP Lists 327 Zend 328 PHP Builder 328 Black Beans 328 PHP Beginners .328 PHP Dev Center 329 PHPMac.com 329 PHP Editors 329 SourceForge.net ... your file system Because PHP began life on the Web, it has many features that are particularly well suited for use in scripts that create dynamic Web pages Currently, you find PHP most often hard at work in Web pages, but its use for other purposes is growing PHP is very popular for Web sites According to the PHP Web site (www .php net/usage .php) , over 11 million domains are using PHP Yahoo!, which is probably... specified when PHP is installed Prior to PHP 5, support for MySQL 4.0 and earlier was included automatically PHP 5 includes support for SQLite by default SQLite provides quick and easy methods for storing and retrieving data in flat files 17 18 Part I: Say Hello to the PHP Scripting Language Previous versions of PHP You should be aware of some significant changes in previous PHP versions because existing scripts... X 339 Before installing on Mac 339 Installing on Mac .340 Installation Options for Unix/Linux/Mac 342 Configuring on Unix/Linux/Mac 343 Installing PHP on Computers Running Windows 3 45 Installing PHP CGI with the PHP installer .346 Installing PHP manually 348 Configuring PHP and your Web server on Windows computers 349 Appendix B: Useful PHP Built-in . the book. 5 Introduction 02 54 1668 intro.qxd 3/ 25/ 04 2 :52 PM Page 5 6 PHP 5 For Dummies 02 54 1668 intro.qxd 3/ 25/ 04 2 :52 PM Page 6 Part I Say Hello to the PHP Scripting Language 03 54 1668 PP01.qxd. 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. strings together 85 Manipulating strings 85 Formatting output strings 89 PHP 5 For Dummies viii 01 54 1668 FM.qxd 3/ 25/ 04 2 :51 PM Page viii Working with Dates and Times 92 Formatting dates 92 Storing

Ngày đăng: 25/03/2014, 16:04

Mục lục

  • Table of Contents

  • Part I: Say Hello to the PHP Scripting Language

    • Chapter 1: Getting to Know PHP

      • Getting Familiar with PHP

      • Considering the Various Uses for PHP

        • Using PHP for Web applications

        • Using PHP for database applications

        • Using PHP with your file system

        • Using PHP for system commands

        • Understanding How PHP Works

          • PHP as a general-purpose language

          • PHP for the Web

          • Keeping Up with Changes in PHP

            • PHP 5

            • Previous versions of PHP

            • Chapter 2: Setting Up the Environment

              • Establishing Your Web Environment

                • Using an existing Web environment

                • Choosing a Web hosting company

                • Setting up your own Web environment

                • Testing PHP

                • Setting Up PHP for General-Purpose Scripting

                • Configuring PHP

                • Using Tools to Build PHP Scripts

                  • Programming editors

                  • Integrated Development Environment (IDE)

                  • Chapter 3: Creating Your First PHP Script

                    • Writing PHP Statements

                    • Building Scripts

                      • Adding PHP statements to HTML pages

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan