PHP 7 Quick Scripting Reference, Second Edition by Mikael Olsson


6358c7695f4e351-261x361.jpg Author Mikael Olsson
Isbn 9781484219218
File size 15.39MB
Year 2016
Pages 136
Language English
File format PDF
Category software



 

PHP 7 Quick Scripting Reference Second Edition Mikael Olsson PHP 7 Quick Scripting Reference Mikael Olsson Hammarland, Finland ISBN-13 (pbk): 978-1-4842-1921-8 DOI 10.1007/978-1-4842-1922-5 ISBN-13 (electronic): 978-1-4842-1922-5 Library of Congress Control Number: 2016941199 Copyright © 2016 by Mikael Olsson This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Steve Anglin Technical Reviewer: Jamie Rumbelow Editorial Board: Steve Anglin, Pramila Balan, Louise Corrigan, Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing Coordinating Editor: Mark Powers Copy Editor: Kim Burton-Weisman Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com/9781484219218. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. Printed on acid-free paper Contents at a Glance About the Author ..............................................................................xv About the Technical Reviewer ........................................................xvii Introduction .....................................................................................xix ■ Chapter 1: Using PHP...................................................................... 1 ■Chapter 2: Variables ....................................................................... 5 ■Chapter 3: Operators ...................................................................... 9 ■Chapter 4: String .......................................................................... 15 ■Chapter 5: Arrays ......................................................................... 19 ■Chapter 6: Conditionals ................................................................ 23 ■Chapter 7: Loops........................................................................... 27 ■Chapter 8: Functions .................................................................... 31 ■Chapter 9: Class ........................................................................... 39 ■Chapter 10: Inheritance ................................................................ 45 ■Chapter 11: Access Levels............................................................ 49 ■Chapter 12: Static ......................................................................... 53 ■Chapter 13: Constants .................................................................. 57 ■Chapter 14: Interface .................................................................... 61 ■Chapter 15: Abstract .................................................................... 65 ■Chapter 16: Traits ......................................................................... 69 iii ■ CONTENTS AT A GLANCE ■Chapter 17: Importing Files .......................................................... 71 ■Chapter 18: Type Declarations ...................................................... 75 ■Chapter 19: Type Conversions ...................................................... 79 ■Chapter 20: Variable Testing......................................................... 81 ■Chapter 21: Overloading ............................................................... 87 ■Chapter 22: Magic Methods ......................................................... 91 ■Chapter 23: User Input.................................................................. 97 ■Chapter 24: Cookies ................................................................... 103 ■Chapter 25: Sessions .................................................................. 105 ■Chapter 26: Namespaces ............................................................ 107 ■Chapter 27: References .............................................................. 113 ■Chapter 28: Advanced Variables ................................................ 117 ■Chapter 29: Error Handling ......................................................... 121 ■Chapter 30: Exception Handling ................................................. 127 ■Chapter 31: Assertions ............................................................... 131 Index .............................................................................................. 133 iv Contents About the Author ............................................................................. xv About the Technical Reviewer ....................................................... xvii Introduction .................................................................................... xix ■Chapter 1: Using PHP...................................................................... 1 Embedding PHP ....................................................................................... 1 Outputting Text ........................................................................................ 2 Installing a Web Server ........................................................................... 3 Hello World .............................................................................................. 3 Compile and Parse .................................................................................. 4 Comments ............................................................................................... 4 ■Chapter 2: Variables ....................................................................... 5 Defining Variables ................................................................................... 5 Data Types ............................................................................................... 5 Integer Type ............................................................................................. 6 Floating-Point Type .................................................................................. 7 Bool Type ................................................................................................. 7 Null Type .................................................................................................. 7 Default Values ......................................................................................... 7 v ■ CONTENTS ■Chapter 3: Operators ...................................................................... 9 Arithmetic Operators ............................................................................... 9 Assignment Operators ............................................................................. 9 Combined Assignment Operators .......................................................... 10 Increment and Decrement Operators .................................................... 10 Comparison Operators........................................................................... 11 Logical Operators .................................................................................. 11 Bitwise Operators .................................................................................. 12 Operator Precedence............................................................................. 12 Additional Logical Operators ................................................................. 13 ■Chapter 4: String .......................................................................... 15 String Concatenation ............................................................................. 15 Delimiting Strings .................................................................................. 15 Heredoc Strings..................................................................................... 16 Nowdoc Strings .................................................................................... 16 Escape Characters ................................................................................ 16 Character Reference ............................................................................. 17 String Compare ..................................................................................... 17 ■Chapter 5: Arrays ......................................................................... 19 Numeric Arrays...................................................................................... 19 Associative Arrays ................................................................................. 20 Mixed Arrays.......................................................................................... 20 Multi-Dimensional Arrays ...................................................................... 21 vi ■ CONTENTS ■Chapter 6: Conditionals ................................................................ 23 If Statement........................................................................................... 23 Switch Statement .................................................................................. 24 Alternative Syntax ................................................................................. 24 Mixed Modes ......................................................................................... 25 Ternary Operator ................................................................................... 25 ■Chapter 7: Loops........................................................................... 27 While Loop............................................................................................. 27 Do-while Loop ....................................................................................... 27 For Loop ................................................................................................ 27 Foreach Loop ......................................................................................... 28 Alternative Syntax ................................................................................. 29 Break ..................................................................................................... 29 Continue ............................................................................................... 29 Goto ....................................................................................................... 30 ■Chapter 8: Functions .................................................................... 31 Defining Functions ................................................................................ 31 Calling Functions ................................................................................... 31 Function Parameters ............................................................................. 32 Default Parameters ............................................................................... 32 Variable Parameter Lists ....................................................................... 33 Return Statement .................................................................................. 34 Scope and Lifetime ............................................................................... 34 Anonymous Functions ........................................................................... 36 vii ■ CONTENTS Closures ................................................................................................ 37 Generators ............................................................................................. 37 Built-in Functions .................................................................................. 38 ■Chapter 9: Class ........................................................................... 39 Instantiating an Object .......................................................................... 40 Accessing Object Members ................................................................... 40 Initial Property Values............................................................................ 40 Constructor............................................................................................ 41 Destructor ............................................................................................. 42 Case Sensitivity ..................................................................................... 42 Object Comparison ................................................................................ 42 Anonymous Classes .............................................................................. 43 Closure Object ....................................................................................... 43 ■Chapter 10: Inheritance ................................................................ 45 Overriding Members .............................................................................. 46 Final Keyword ....................................................................................... 47 Instanceof Operator ............................................................................... 47 ■Chapter 11: Access Levels............................................................ 49 Private Access ....................................................................................... 49 Protected Access ................................................................................... 50 Public Access ....................................................................................... 50 Var Keyword ......................................................................................... 50 Object Access ........................................................................................ 50 Access Level Guideline.......................................................................... 51 viii ■ CONTENTS ■Chapter 12: Static ......................................................................... 53 Referencing Static Members ................................................................. 53 Static Variables...................................................................................... 54 Late Static Bindings .............................................................................. 55 ■Chapter 13: Constants .................................................................. 57 Const ..................................................................................................... 57 Define .................................................................................................... 58 Const and define ................................................................................... 58 Constant Guideline ................................................................................ 59 Magic Constants.................................................................................... 59 ■Chapter 14: Interface .................................................................... 61 Interface Signatures .............................................................................. 61 Interface Example ................................................................................. 62 Interface Usages ................................................................................... 63 Interface Guideline ................................................................................ 63 ■Chapter 15: Abstract .................................................................... 65 Abstract Methods .................................................................................. 65 Abstract Example .................................................................................. 65 Abstract Classes and Interfaces ............................................................ 66 Abstract Guideline ................................................................................. 67 ■Chapter 16: Traits ......................................................................... 69 Inheritance and Traits ............................................................................ 70 Trait Guidelines...................................................................................... 70 ix ■ CONTENTS ■Chapter 17: Importing Files .......................................................... 71 Include Path .......................................................................................... 71 Require .................................................................................................. 72 Include_once ......................................................................................... 72 Require_once ........................................................................................ 72 Return.................................................................................................... 73 _Autoload .............................................................................................. 73 ■Chapter 18: Type Declarations ...................................................... 75 Argument Type Declarations ................................................................. 75 Return Type Declarations ...................................................................... 77 Strict Typing .......................................................................................... 77 ■Chapter 19: Type Conversions ...................................................... 79 Explicit Casts ......................................................................................... 79 Set type .................................................................................................. 80 Get type .................................................................................................. 80 ■Chapter 20: Variable Testing......................................................... 81 Isset ....................................................................................................... 81 Empty .................................................................................................... 81 Is_null.................................................................................................... 82 Unset ..................................................................................................... 82 Null Coalescing Operator ....................................................................... 83 Determining Types................................................................................. 83 Variable Information .............................................................................. 84 x ■ CONTENTS ■Chapter 21: Overloading ............................................................... 87 Property Overloading ............................................................................. 87 Method Overloading .............................................................................. 88 Isset and unset Overloading .................................................................. 88 ■Chapter 22: Magic Methods ......................................................... 91 _ToString ............................................................................................... 92 _Invoke .................................................................................................. 93 Object Serialization ............................................................................... 93 _Sleep ................................................................................................... 94 _Wakeup ............................................................................................... 94 Set State................................................................................................ 94 Object Cloning ....................................................................................... 95 ■Chapter 23: User Input.................................................................. 97 HTML Form ............................................................................................ 97 Sending with POST ................................................................................ 97 Sending with GET .................................................................................. 98 Request Array ........................................................................................ 98 Security Concerns ................................................................................. 98 Submitting Arrays .................................................................................. 99 File Uploading...................................................................................... 100 Superglobals ....................................................................................... 101 ■Chapter 24: Cookies ................................................................... 103 Creating Cookies ................................................................................. 103 Cookie Array ........................................................................................ 103 Deleting Cookies ................................................................................. 103 xi ■ CONTENTS ■Chapter 25: Sessions .................................................................. 105 Starting a Session ............................................................................... 105 Session Array ...................................................................................... 105 Deleting a Session............................................................................... 106 ■Chapter 26: Namespaces ............................................................ 107 Creating Namespaces ......................................................................... 107 Nested Namespaces ........................................................................... 108 Alternative Syntax ............................................................................... 108 Referencing Namespaces ................................................................... 109 Namespace Aliases ............................................................................. 110 Namespace Keyword .......................................................................... 111 Namespace Guideline.......................................................................... 112 ■Chapter 27: References .............................................................. 113 Assign by Reference............................................................................ 113 Pass by Reference ............................................................................... 113 Return by Reference............................................................................ 115 ■Chapter 28: Advanced Variables ................................................ 117 Curly Syntax ........................................................................................ 117 Variable Variable Names...................................................................... 118 Variable Function Names .................................................................... 118 Variable Class Names .......................................................................... 119 ■Chapter 29: Error Handling ......................................................... 121 Correcting Errors ................................................................................. 121 Error Levels ......................................................................................... 122 Error-Handling Environment ................................................................ 123 xii ■ CONTENTS Custom Error Handlers ........................................................................ 124 Raising Errors ...................................................................................... 125 ■Chapter 30: Exception Handling ................................................. 127 Try-catch Statement ............................................................................ 127 Throwing Exceptions ........................................................................... 127 Catch Block ......................................................................................... 128 Finally Block ........................................................................................ 128 Rethrowing Exceptions........................................................................ 129 Uncaught Exception Handler ............................................................... 129 Errors and Exceptions ......................................................................... 129 ■Chapter 31: Assertions ............................................................... 131 Assert Performance............................................................................. 131 Index .............................................................................................. 133 xiii About the Author Mikael Olsson is a professional web entrepreneur, programmer, and author. He works for an R&D company in Finland, where he specializes in software development. In his spare time, Mikael writes books and creates web sites on his various fields of interest. The books that he writes are focused on efficiently teaching the subject by explaining only what is relevant and practical, without any unnecessary repetition or theory. xv About the Technical Reviewer Jamie Rumbelow is a freelance web developer and an aspiring academic. He’s the author of three books on CodeIgniter and is a keen public speaker. He has worked on dozens of web applications during his eight years freelancing. Jamie lives in London, England. xvii Introduction PHP is a server-side programming language used for creating dynamic web sites and interactive web applications. The acronym PHP originally stood for Personal Home Page, but as its functionality grew, this was changed to PHP: Hypertext Preprocessor. This recursive acronym comes from the fact that it takes PHP code as input and produces HTML as output. This means that users do not need to install any software to view PHP-generated web pages. All that is required is that the web server has PHP installed to interpret the script. In contrast with HTML sites, PHP sites are dynamically generated. Instead of the site being made up of a large number of static HTML files, a PHP site may consist of only a handful of template files. The template files describe only the structure of the site using PHP code, while the web content is pulled from a database and the style formatting is from Cascading Style Sheets (CSS). This allows for site-wide changes from a single location, providing a flexible web site that is easy to design, maintain, and update. When creating web sites with PHP, a content management system (CMS) is generally used. A CMS provides a fully integrated platform for web site development consisting of a back end and a front end. The front end is what visitors see when they arrive at the site, whereas the back end is where the site is configured, updated, and managed by an administrator. The back end also allows a web developer to change template files and modify plugins to more extensively customize the functionality and structure of the site. Examples of free PHP-based CMS solutions include WordPress, Joomla, ModX, and Drupal, with WordPress being the most popular and accounting for more than half of the CMS market. The first version of PHP was created by Rasmus Lerdorf and released in 1995. Since then, PHP has evolved greatly from a simple scripting language to a fully featured web programming language. The official implementation is now released by The PHP Group, with PHP 7 being the most recent version as of this writing. The language may be used free of charge and is open source, allowing developers to extend it for their own use or to contribute to its development. PHP is by far the most popular server-side programming language in use today. It holds a growing 80% market share when compared with other server-side technologies, such as ASP.NET, Java, Ruby, and Perl. One of the reasons for the widespread adoption of PHP is its platform independence. It can be installed on all major web servers and operating systems, and used with any major database system. Another strong feature of PHP is its simple-to-use syntax based on C and Perl, which is easy for a newcomer to learn; however, PHP also offers many advanced features for the professional programmer. xix

Author Mikael Olsson Isbn 9781484219218 File size 15.39MB Year 2016 Pages 136 Language English File format PDF Category Software Book Description: FacebookTwitterGoogle+TumblrDiggMySpaceShare This pocket reference guide has been updated with the new PHP 7.0 release. It is a condensed, code-rich scripting and syntax handbook for the PHP scripting language. PHP 7 Quick Scripting Reference presents the essential PHP script in a well-organized format. You won’t find any technical jargon, bloated samples, drawn out history lessons or witty stories in this book. What you will find is a Web scripting language reference that is concise, to the point and highly accessible. The book is packed with useful information and is a must-have for any PHP programmer or Web developer. In it, you will find a concise reference to the PHP 7 scripting language syntax. It includes short, simple and focused code examples and a well laid out table of contents and a comprehensive index allowing easy review.   What you’ll learn What is new in PHP 7 and how to get started with it What are variables, operators, strings, arrays, conditionals, loops and other language constructs How to group and reuse code with functions, methods and namespaces How to use object-oriented features such as classes, inheritance, abstract classes and interfaces How to import files and retrieve user data What are type declarations and type conversions How to test variables, create references and use overloading methods How to store user data with cookies and sessions How to deal with errors through error handling, exception handling and assertions Who this book is for This book is a handy, pocket quick scripting syntax reference for experienced PHP as well as perhaps other programmers and Web developers even new to PHP.<     Download (15.39MB) CSS Quick Syntax Reference Beginning PHP 5.3 Zend Framework in Action PHP Object-Oriented Solutions PHP Programming with PEAR Load more posts

Leave a Reply

Your email address will not be published. Required fields are marked *