Programming PHP, 3rd edition

540 184 0
Programming PHP, 3rd edition

Đ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

www.it-ebooks.info www.it-ebooks.info THIRD EDITION Programming PHP Kevin Tatroe, Peter MacIntyre, and Rasmus Lerdorf Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo www.it-ebooks.info 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 www.it-ebooks.info I would like to dedicate my portions of this book to my wonderful wife, Dawn Etta Riley I love you Dawn! —Peter MacIntyre www.it-ebooks.info www.it-ebooks.info 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 www.it-ebooks.info 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 63 vi | Table of Contents www.it-ebooks.info 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 www.it-ebooks.info 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 www.it-ebooks.info 119 120 120 122 122 122 122 123 123 124 125 125 126 126 128 128 128 129 129 130 131 include_once function, 309 include_once keyword, 58 indexed arrays, 119 inequality operator (!= or ), 40 inet_ntop function, 432 inet_pton function, 432 inheritance, 148, 155–156 ini_get function, 433 ini_get_all function, 433 ini_restore function, 433 ini_set function, 352, 433 inline options, in regular expressions, 109 input, filtering, 289–291 INSERT command, SQL, 204, 209 installation, instanceof operator, 47, 156 insteadof keyword, 159 (int) operator, 44 (integer) operator, 44 integers, 22–23 Integrated Development Environment (IDE), 355–357 interfaces, 156–157 interface_exists function, 433 interpolation of vairables, 77–78 introspection, 163 intval function, 434 in_array function, 133, 432 ip2long function, 434 isset function, 34, 126, 437 is_a function, 434 is_array function, 27, 434 is_bool function, 26, 434 is_callable function, 434 is_dir function, 435 is_executable function, 435 is_file function, 435 is_finite function, 435 is_float function, 24, 435 is_infinite function, 435 is_int function, 23, 435 is_integer function, 23 is_link function, 436 is_nan function, 436 is_null function, 29, 436 is_numeric function, 436 is_object function, 28, 164, 436 is_readable function, 222, 436 is_real function, 24 is_resource function, 28, 436 is_scalar function, 437 is_string function, 25, 437 is_subclass_of function, 437 is_uploaded_file function, 301, 437 is_writable function, 222, 437 iterator functions, 130 Iterator interface, 143–145 J join function, 97 JSON (JavaScript Object Notation), 339 JsonSerializable interface, 340 json_decode function, 339 json_encode function, 339 K Kennedy, Bill (author) HTML & XHTML: The Definitive Guide, xviii key function, 130, 438 key method, 144 keywords, 15, 21–22 Kline, Keven (author) SQL in a Nutshell (O’Reilly), 205 krsort function, 135, 438 ksort function, 135, 438 L labels, 56 Lane, David (author) Web Database Applications with PHP and MySQL, 2nd Edition (O’Reilly), 203 lcg_value function, 438 lchgrp function, 438 lchown function, 439 Learning XML (O’Reilly), 269 left angle bracket ( tag, preceding XML document, 268, 269 XML-RPC, 267, 344–347 clients, 346–347 servers, 344–346 xmlrpc extension, 344 xmlrpc-epi distribution, 346 xml_error_string function, 278 xml_get_error_code function, 278 xml_parse function, 277 xml_parser_create function, 276–277 xml_parser_free function, 276 xml_parser_get_option function, 276 xml_parser_set_option function, 275 xml_set_character_data_handler function, 272 xml_set_default_handler function, 275 xml_set_element_handler function, 271–272 xml_set_external_entity_ref_handler function, 274 xml_set_notation_decl_handler function, 275 xml_set_object function, 279 xml_set_processing_instruction_handler function, 272–273 xml_set_unparsed_entity_decl_handler function, 275 XOR operator, bitwise (^), 42 XOR operator, logical (xor), 43 Index | 513 www.it-ebooks.info XOR-equals operator, bitwise (^=), 46 XSLT (Extensible Stylesheet Language Transformations), 285–287 XSS (cross-site scripting), 292–294 Z Zend Server CE, 223, 329 Zend Studio for Eclipse, 355 zend_logo_guid function, 490 zend_thread_id function, 490 zend_version function, 490 514 | Index www.it-ebooks.info About the Authors Kevin Tatroe has been a Macintosh/OS X and web stack programmer for over 20 years and an iOS developer since the dawn of the iOS SDK, developing websites and apps both small and enormous He’s attracted to technologies that allow for rapid iteration and experimentation Kevin, his wife Jenn, his son Hadden, and their two cats live on the edge of the rural plains of Colorado, just far enough away from the mountains to avoid the worst snowfall and just close enough to the foothills to avoid tornadoes (most of the time) The house is filled with LEGO creations, board games, and numerous other distractions Peter MacIntyre has over 23 years of experience in the information technology industry, primarily in the area of PHP and Web Technologies He has contributed writing material for many IT industry publications: author of PHP: The Good Parts, and coauthor of Pro PHP Programming (APress), Using Visual Objects, Using PowerBuilder 5, ASP.NET Bible, and Zend Studio for Eclipse Developer’s Guide Peter has spoken several times at North American and international computer conferences, including CA World in New Orleans; CA TechniCon in Cologne, Germany; and CA Expo in Melbourne, Australia Peter lives and works in Prince Edward Island, Canada, and he is a Zend Certified Engineer (PHP 5.3) Rasmus Lerdorf started the PHP Project back in 1995 and has been actively involved in PHP development ever since Also involved in a number of other open source projects, Rasmus is a longtime Apache contributor and foundation member He is the author of the first edition of the PHP Pocket Reference, and the co-author of the first edition of Programming PHP Colophon The animal on the cover of Programming PHP, Third Edition is a cuckoo (Cuculus canorus) Cuckoos epitomize minimal effort The common cuckoo doesn’t build a nest—instead, the female cuckoo finds another bird’s nest that already contains eggs and lays an egg in it (a process she may repeat up to 25 times, leaving egg per nest) The nest mother rarely notices the addition, and usually incubates the egg and then feeds the hatchling as if it were her own Why don’t nest mothers notice that the cuckoo’s eggs are different from their own? Recent research suggests that it’s because all eggs look the same in the ultraviolet spectrum, in which birds can see When they hatch, the baby cuckoos push all the other eggs out of the nest If the other eggs hatched first, the babies are pushed out too The host parents often continue to feed the cuckoo even after it grows to be much larger than they are, and cuckoo chicks sometimes use their call to lure other birds to feed them as well Interestingly, only Old World (European) cuckoos colonize other nests—the New World (American) cuckoos www.it-ebooks.info build their own (untidy) nests Like many Americans, these cuckoos migrate to the tropics for winter Cuckoos have a long and glorious history in literature and the arts The Bible mentions them, as Pliny and Aristotle Beethoven used the cuckoo’s distinctive call in his Pastoral Symphony And here’s a bit of etymology for you: the word “cuckold” (a husband whose wife is cheating on him) comes from “cuckoo.” Presumably, the practice of laying one’s eggs in another’s nest seemed an appropriate metaphor The cover image is from the Dover Pictorial Archive The cover font is Adobe ITC Garamond The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSans Mono Condensed www.it-ebooks.info ...www.it-ebooks.info THIRD EDITION Programming PHP Kevin Tatroe, Peter MacIntyre, and Rasmus Lerdorf Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo www.it-ebooks.info Programming PHP, Third Edition by... 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... 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

Ngày đăng: 12/03/2019, 11:18

Mục lục

    Assumptions This Book Makes

    Contents of This Book

    Conventions Used in This Book

    How to Contact Us

    What Does PHP Do?

    A Brief History of PHP

    The Evolution of PHP

    The Widespread Use of PHP

    A Walk Through PHP

    Whitespace and Line Breaks

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

Tài liệu liên quan