ebook img

The PHP Workshop: Learn to build interactive applications and kickstart your career as a web developer PDF

603 Pages·2019·8.599 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The PHP Workshop: Learn to build interactive applications and kickstart your career as a web developer

The PHP Workshop A New, Interactive Approach to Learning PHP Alexandru Busuioc, David Carr, Markus Gray, Vijay Joshi, Mark McCollum, Bart McLeod, and M A Hossain Tonu The PHP Workshop Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Authors: Alexandru Busuioc, David Carr, Markus Gray, Vijay Joshi, Mark McCollum, Bart McLeod, and M A Hossain Tonu Technical Reviewers: Jordi Martinez and Kristian Secor Managing Editor: Rutuja Yerunkar Acquisitions Editor: Sarah Lawton Production Editor: Samita Warang Editorial Board: Shubhopriya Banerjee, Bharat Botle, Ewan Buckingham, Megan Carlisle, Mahesh Dhyani, Manasa Kumar, Alex Mazonowicz, Bridget Neale, Dominic Pereira, Shiny Poojary, Abhishek Rane, Erol Staveley, Ankita Thakur, Nitesh Thakur, and Jonathan Wray First Published: October 2019 Production Reference: 2201119 ISBN: 978-1-83864-891-6 Published by Packt Publishing Ltd. Livery Place, 35 Livery Street Birmingham B3 2PB, UK Table of Contents Preface i Chapter 1: Introducing PHP 1 Introduction .................................................................................................... 2 Getting Started with PHP Web Development ............................................. 2 Built-in Templating Engine .................................................................................. 3 PHP in the Interactive Shell ................................................................................ 4 Exercise 1.1: Printing Hello World to the Standard Output ............................ 4 Exercise 1.2: Printing Hello World by Executing a PHP File ............................ 6 Assigning and Using Variables ........................................................................... 7 Creating and Assigning Variables to Print Simple Messages on the Web Browser ........................................................................................... 8 Exercise 1.3: Using Input Variables to Print Simple Strings ............................ 9 Exercise 1.4: Using the Built-in Server to Print a String ................................ 11 HyperText Markup Language ..................................................................... 12 Cascading Style Sheets ................................................................................ 15 Exercise 1.5: Creating a Login Form Page Using Bootstrap .......................... 18 Exercise 1.6: Printing PHP Code Output between HTML Tags ...................... 20 Using the Server Variable ........................................................................... 21 Exercise 1.7: Displaying Server Information ................................................... 21 Other Predefined Variables .............................................................................. 22 Assignment by Value and by Reference .......................................................... 23 Exercise 1.8: Assigning a Variable by Reference and Changing its Value ... 23 Using isset to Check for Variable Declaration ................................................ 25 Exercise 1.9: Using isset to Check whether a Variable Has Been Set .......... 26 Activity 1.1: Displaying Query Strings in the Browser ................................... 27 Summary ....................................................................................................... 28 Chapter 2: Types and Operators 31 Introduction .................................................................................................. 32 What are Data Types? .................................................................................. 32 Integers ............................................................................................................... 32 Strings .................................................................................................................. 33 Single and Double-Quoted Strings ................................................................... 33 Heredoc and Nowdoc Syntaxes ....................................................................... 35 Floats ................................................................................................................... 37 Boolean ............................................................................................................... 37 Exercise 2.1: Using Simple Data Types ........................................................... 38 Arrays .................................................................................................................. 40 Indexed and Associative Arrays ....................................................................... 41 Adding and Removing Items from an Array ................................................... 45 Exercise 2.2: Creating a Multidimensional Array .......................................... 48 Scalar Types .................................................................................................. 51 Type Conversion ................................................................................................. 52 Exercise 2.3: Converting a Boolean to an Integer .......................................... 54 Exercise 2.4: Converting an Integer to a String ............................................. 55 Exercise 2.5: Converting Centimeters to Meters ............................................ 57 Operators and Expressions ........................................................................ 58 Arithmetic Operators ........................................................................................ 59 String Operators ................................................................................................ 59 Bitwise Operators .............................................................................................. 60 Assignment Operators ...................................................................................... 61 Comparison Operators ...................................................................................... 62 Increment/Decrement Operators .................................................................... 63 Logical Operators ............................................................................................... 63 Array Operators ................................................................................................. 64 Conditional Assignment Operators ................................................................. 64 Activity 2.1: Printing the BMI of a User ........................................................... 64 Summary ....................................................................................................... 65 Chapter 3: Control Statements 67 Introduction .................................................................................................. 68 Boolean Expressions ................................................................................... 70 Boolean Constants ....................................................................................... 71 Logical Operators ......................................................................................... 71 The not Operator ............................................................................................... 72 The and Operator .............................................................................................. 72 The or Operator ................................................................................................. 72 The xor Operator ............................................................................................... 73 Short-Circuit Evaluation and Operator Precedence ...................................... 73 The Precedence of Logical Operators ............................................................. 74 || versus or ...................................................................................................74 && versus and ...............................................................................................75 Comparison Operators ...................................................................................... 76 Branching ...................................................................................................... 77 The if Statement ................................................................................................. 78 The if…else Statement ....................................................................................... 79 Exercise 3.1: Creating a Basic Script to Implement the if...else Test Case .. 80 Exercise 3.2: Implementing the Nested if...else Structure ............................ 81 The Ternary Operator ....................................................................................... 83 The if…elseif…else Statement .......................................................................... 83 Exercise 3.3: Creating a Script Using the if... elseif... else Statement .......... 85 The switch Case .................................................................................................. 86 Exercise 3.4: Creating a Script to Implement a Switch Case ......................... 90 Looping .......................................................................................................... 92 Bounded Loops versus Unbounded Loops ..................................................... 92 The while Loop ................................................................................................... 93 Exercise 3.5: Printing the Numbers 1 to 10 Using a while Loop ................... 94 The do…while Loop ............................................................................................ 96 Exercise 3.6: Converting a while Loop to a do...while Loop .......................... 97 The for Loop ....................................................................................................... 98 Exercise 3.7: Using a for Loop to Print the Days of the Week ................... 100 The foreach Loop ............................................................................................ 101 Exercise 3.8: Using a foreach Loop to Print the Days of the Week .......... 102 Nesting Loops .................................................................................................. 103 Exercise 3.9: Using Nested foreach Loops ................................................... 105 The break Statement ...................................................................................... 108 Exercise 3.10: Using a break Statement to Terminate a Loop's Execution .......................................................................................... 109 The continue Statement ................................................................................ 110 Exercise 3.11: Using continue to Skip an Item in a List .............................. 110 Alternative Control Syntaxes ........................................................................ 112 Using System Variables .................................................................................. 113 Activity 3.1: Creating a Movie Listing Script to Print Movies per Director .......................................................................... 113 Tips for Control Structures ............................................................................ 114 Summary .................................................................................................... 115 Chapter 4: Functions 117 Introduction ................................................................................................ 118 What is a Callable? ..................................................................................... 119 Exercise 4.1: Using Built-in Functions ........................................................... 121 Types of Callables ........................................................................................... 124 Language Constructs ................................................................................. 125 Introduction to Built-In Functions ........................................................... 126 Finding Built-In Functions .............................................................................. 127 Parameters and Return Values ..................................................................... 128 Passing Parameters by Reference ................................................................ 129 Passing Scalar Variables by Reference ......................................................... 130 Optional Parameters ...................................................................................... 133 Exercise 4.2: Working with print_r() .............................................................. 134 A Varying Number of Parameters ................................................................. 135 Flag Parameters .............................................................................................. 136 Exercise 4.3: Using Built-In Functions with Arrays ...................................... 137 Introduction to User-Defined Functions ................................................. 142 Naming Functions ........................................................................................... 142 Documenting Your Functions ........................................................................ 142 Namespaced Functions .................................................................................. 143 Pure Functions ................................................................................................ 144 Scope ................................................................................................................ 144 The $GLOBALS Superglobal Array ................................................................. 145 Exercise 4.4: Using the $GLOBALS array ...................................................... 146 The Single Responsibility Principle ............................................................... 147 The function Keyword .................................................................................... 148 Identifier .......................................................................................................... 148 Type Hints ........................................................................................................ 149 The Spread Operator (…) with Type Hints ................................................... 149 Parameters in User-Defined Functions ........................................................ 151 Return Types in User-Defined Functions ..................................................... 152 Signature .......................................................................................................... 152 Returning a Value ........................................................................................... 153 Parameters and Arguments .......................................................................... 153 Optional Parameters ...................................................................................... 154 Parameters Passed by Reference to Our Function ..................................... 155 Default Values for Parameters ...................................................................... 155 Exercise 4.5: Writing a Function that Adds Two Numbers ......................... 156 Variable Functions .......................................................................................... 158 Anonymous Functions .................................................................................... 158 Using a Variable from Outside of Scope Inside an Anonymous Function ................................................................................ 159 Exercise 4.6: Working with Anonymous Functions ..................................... 159 Exercise 4.7: Creating Variable Functions .................................................... 161 Exercise 4.8: Playing with Functions ............................................................. 162 Activity 4.1: Creating a Calculator ................................................................. 164 Summary ..................................................................................................... 165 Chapter 5: Object-Oriented Programming 167 Introduction ................................................................................................ 168 The Object-Oriented Approach ................................................................ 169 OOP Concepts .................................................................................................. 170 Classes ............................................................................................................. 172 Instantiating a Class ...................................................................................... 173 Class Attributes ............................................................................................... 174 Class Constants ............................................................................................... 176 The $this Variable ........................................................................................... 177 Class Methods ................................................................................................. 177 Exercise 5.1: Using the Getter and Setter Methods .................................... 179 Constructor ...................................................................................................... 182 Destructor ........................................................................................................ 183 Exercise 5.2: Instantiating the Class and Printing the Details ................... 185 Inheritance ...................................................................................................... 189 Exercise 5.3: Implementing Inheritance ...................................................... 192 Access Modifiers ............................................................................................ 195 Exercise 5.4: Applying Access Modifiers ....................................................... 198 Static Fields and Methods .............................................................................. 202 parent:: and self:: ............................................................................................ 204 Exercise 5.5: Applying a Static Member ....................................................... 205 Class Abstraction ............................................................................................ 206 Exercise 5.6: Implementing an Abstract Class ............................................. 208 Interfaces .................................................................................................... 213 Exercise 5.7: Implementing an Interface ..................................................... 216 Abstract Classes versus Interfaces ............................................................... 219 Class Type Hinting Plays a Role in Dependency Injection .......................... 220 Overriding .................................................................................................. 222 Attribute Overriding ....................................................................................... 222 Method Overriding ......................................................................................... 222 Exercise 5.8: Overriding an Inherited Method ............................................ 223 Overloading ..................................................................................................... 225 Attribute Overloading .................................................................................... 227 Method Overloading ....................................................................................... 230

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.