PHP: Hypertext Preprocessor, thường được viết tắt thành PHP là một ngôn ngữ lập trình kịch bản hay một loại mã lệnh chủ yếu được dùng để phát triển các ứng dụng viết cho máy chủ, mã nguồn mở, dùng cho mục đích tổng quát. Nó rất thích hợp với web và có thể dễ dàng nhúng vào trang HTML.
THIRD EDITION Programming PHP Kevin Tatroe, Peter MacIntyre, and Rasmus Lerdorf Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo Programming PHP, Third Edition by Kevin Tatroe, Peter MacIntyre, and Rasmus Lerdorf Copyright © 2013 Kevin Tatroe, Peter MacIntyre 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 (http://my.safaribooksonline.com) For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com Editors: Meghan Blanchette and Rachel Roumeliotis Production Editor: Rachel Steely Copyeditor: Kiel Van Horn Proofreader: Emily Quill February 2013: Indexer: Angela Howard Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Rebecca Demarest Third Edition Revision History for the Third Edition: 2013-02-05 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449392772 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Programming PHP, 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 authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-1-449-39277-2 [LSI] 1360094505 I would like to dedicate my portions of this book to my wonderful wife, Dawn Etta Riley I love you Dawn! —Peter MacIntyre Table of Contents Foreword xv Preface xvii Introduction to PHP What Does PHP Do? A Brief History of PHP The Evolution of PHP The Widespread Use of PHP Installing PHP A Walk Through PHP Configuration Page Forms Databases Graphics 2 7 10 13 Language Basics 15 Lexical Structure Case Sensitivity Statements and Semicolons Whitespace and Line Breaks Comments Literals Identifiers Keywords Data Types Integers Floating-Point Numbers Strings Booleans Arrays 15 15 15 16 17 20 20 21 22 22 23 24 25 26 v Objects Resources Callbacks NULL Variables Variable Variables Variable References Variable Scope Garbage Collection Expressions and Operators Number of Operands Operator Precedence Operator Associativity Implicit Casting Arithmetic Operators String Concatenation Operator Auto-increment and Auto-decrement Operators Comparison Operators Bitwise Operators Logical Operators Casting Operators Assignment Operators Miscellaneous Operators Flow-Control Statements if switch while for foreach try catch declare exit and return goto Including Code Embedding PHP in Web Pages Standard (XML) Style SGML Style ASP Style Script Style Echoing Content Directly 27 28 29 29 29 30 30 31 33 34 36 36 37 37 38 38 39 40 41 43 43 45 46 47 47 49 51 53 54 55 55 56 56 57 58 59 60 61 61 61 Functions 63 Calling a Function vi | Table of Contents 63 Defining a Function Variable Scope Global Variables Static Variables Function Parameters Passing Parameters by Value Passing Parameters by Reference Default Parameters Variable Parameters Missing Parameters Type Hinting Return Values Variable Functions Anonymous Functions 64 66 67 68 68 69 69 70 70 71 72 72 73 74 Strings 77 Quoting String Constants Variable Interpolation Single-Quoted Strings Double-Quoted Strings Here Documents Printing Strings echo print() printf() print_r() and var_dump() Accessing Individual Characters Cleaning Strings Removing Whitespace Changing Case Encoding and Escaping HTML URLs SQL C-String Encoding Comparing Strings Exact Comparisons Approximate Equality Manipulating and Searching Strings Substrings Miscellaneous String Functions Decomposing a String String-Searching Functions 77 77 78 78 79 80 81 81 81 83 85 85 85 86 86 87 89 90 91 92 92 93 94 95 96 97 98 Table of Contents | vii Regular Expressions The Basics Character Classes Alternatives Repeating Sequences Subpatterns Delimiters Match Behavior Character Classes Anchors Quantifiers and Greed Noncapturing Groups Backreferences Trailing Options Inline Options Lookahead and Lookbehind Cut Conditional Expressions Functions Differences from Perl Regular Expressions 100 101 102 103 103 104 104 105 105 106 107 108 108 108 109 110 111 112 112 117 Arrays 119 Indexed Versus Associative Arrays Identifying Elements of an Array Storing Data in Arrays Adding Values to the End of an Array Assigning a Range of Values Getting the Size of an Array Padding an Array Multidimensional Arrays Extracting Multiple Values Slicing an Array Splitting an Array into Chunks Keys and Values Checking Whether an Element Exists Removing and Inserting Elements in an Array Converting Between Arrays and Variables Creating Variables from an Array Creating an Array from Variables Traversing Arrays The foreach Construct The Iterator Functions Using a for Loop viii | Table of Contents 119 120 120 122 122 122 122 123 123 124 125 125 126 126 128 128 128 129 129 130 131 ... Introduction to PHP What Does PHP Do? A Brief History of PHP The Evolution of PHP The Widespread Use of PHP Installing PHP A Walk Through PHP Configuration... the phpinfo() page | Chapter 1: Introduction to PHP Example 1-2 Using phpinfo() < ?php phpinfo();?> Figure 1-3 shows the first part of the output of Example 1-2 Figure 1-3 Partial output of phpinfo()... this example, the PHP code is placed between the < ?php and ?> tags There are other ways to tag your PHP code—see Chapter for a full description Configuration Page The PHP function phpinfo() creates