0%

Book Description

Cut through the noise and get real results with a step-by-step approach to learning PHP programming

Key Features

  • Ideal for the PHP beginner who is getting started for the first time
  • Complete step-by-step exercises and open-ended activities to help build and reinforce key skills
  • Structured to let you progress at your own pace, on your own terms
  • Use your physical copy to redeem free access to the online interactive edition

Book Description

You already know you want to learn PHP 7, and a smarter way to learn PHP development is to learn by doing. The PHP Workshop focuses on building up your practical skills so that you can develop cutting-edge, high-performance web applications. It's ideal if you're looking to work with an existing application, or even develop your own side project with a PHP framework like Laravel. You'll learn from real examples that lead to real results.

Throughout The PHP Workshop, you'll take an engaging step-by-step approach to understanding PHP development. You won't have to sit through any unnecessary theory. If you're short on time you can jump into a single exercise each day or spend an entire weekend learning about third-party libraries. It's your choice. Learning on your terms, you'll build up and reinforce key skills in a way that feels rewarding.

Every physical copy of The PHP Workshop unlocks access to the interactive edition. With videos detailing all exercises and activities, you'll always have a guided solution. You can also benchmark yourself against assessments, track progress, and receive content updates. It's a premium learning experience that's included with your printed copy. To redeem, follow the instructions located at the start of your PHP guide.

Fast-paced and direct, The PHP Workshop is the ideal companion for PHP beginners. You'll build and iterate on your code like a software developer, learning along the way. This process means that you'll find that your new skills stick, embedded as best practice. A solid foundation for the years ahead.

What you will learn

  • Get to grips with fundamental concepts and conventions of PHP 7
  • Work with PHP MySQL databases and build dynamic applications
  • Learn how to compile logical errors and handle exceptions in your programs
  • Explore how PHP unit testing can help create a bug-free development process
  • Integrate third-party packages into your applications
  • Connect external web services to your applications with tools like Guzzle

Who this book is for

Workshops are ideal for foundational, entry-level developers who are learning something new.

Table of Contents

  1. Preface
    1. About the Book
      1. About the Chapters
      2. Conventions
      3. Before You Begin
      4. Installing PHP 7.3 (Ubuntu)
      5. Installing PHP 7.3 (Windows)
      6. Installing MySQL 5.7 (Ubuntu)
      7. Accessing MySQL as Root (Using sudo)
      8. Creating a Test User
      9. Granting all Privileges on a Test User
      10. Installing MySQL Workbench
      11. Installing MySQL 5.7 (Windows)
      12. Installing Composer
      13. Installing the Insomnia REST Client
      14. Installing the Code Bundle
  2. 1. Introducing PHP
    1. Introduction
    2. Getting Started with PHP Web Development
      1. Built-in Templating Engine
      2. PHP in the Interactive Shell
      3. Exercise 1.1: Printing Hello World to the Standard Output
      4. Exercise 1.2: Printing Hello World by Executing a PHP File
      5. Assigning and Using Variables
      6. Creating and Assigning Variables to Print Simple Messages on the Web Browser
      7. Exercise 1.3: Using Input Variables to Print Simple Strings
      8. Exercise 1.4: Using the Built-in Server to Print a String
    3. HyperText Markup Language
    4. Cascading Style Sheets
      1. Exercise 1.5: Creating a Login Form Page Using Bootstrap
      2. Exercise 1.6: Printing PHP Code Output between HTML Tags
    5. Using the Server Variable
      1. Exercise 1.7: Displaying Server Information
      2. Other Predefined Variables
      3. Assignment by Value and by Reference
      4. Exercise 1.8: Assigning a Variable by Reference and Changing its Value
      5. Using isset to Check for Variable Declaration
      6. Exercise 1.9: Using isset to Check whether a Variable Has Been Set
      7. Activity 1.1: Displaying Query Strings in the Browser
    6. Summary
  3. 2. Types and Operators
    1. Introduction
    2. What are Data Types?
      1. Integers
      2. Strings
      3. Single and Double-Quoted Strings
      4. Heredoc and Nowdoc Syntaxes
      5. Floats
      6. Boolean
      7. Exercise 2.1: Using Simple Data Types
      8. Arrays
      9. Indexed and Associative Arrays
      10. Adding and Removing Items from an Array
      11. Exercise 2.2: Creating a Multidimensional Array
    3. Scalar Types
      1. Type Conversion
      2. Exercise 2.3: Converting a Boolean to an Integer
      3. Exercise 2.4: Converting an Integer to a String
      4. Exercise 2.5: Converting Centimeters to Meters
    4. Operators and Expressions
      1. Arithmetic Operators
      2. String Operators
      3. Bitwise Operators
      4. Assignment Operators
      5. Comparison Operators
      6. Increment/Decrement Operators
      7. Logical Operators
      8. Array Operators
      9. Conditional Assignment Operators
      10. Activity 2.1: Printing the BMI of a User
    5. Summary
  4. 3. Control Statements
    1. Introduction
    2. Boolean Expressions
    3. Boolean Constants
    4. Logical Operators
      1. The not Operator
      2. The and Operator
      3. The or Operator
      4. The xor Operator
      5. Short-Circuit Evaluation and Operator Precedence
      6. The Precedence of Logical Operators
        1. || versus or
        2. && versus and
      7. Comparison Operators
    5. Branching
      1. The if Statement
      2. The if…else Statement
      3. Exercise 3.1: Creating a Basic Script to Implement the if...else Test Case
      4. Exercise 3.2: Implementing the Nested if...else Structure
      5. The Ternary Operator
      6. The if…elseif…else Statement
      7. Exercise 3.3: Creating a Script Using the if... elseif... else Statement
      8. The switch Case
      9. Exercise 3.4: Creating a Script to Implement a Switch Case
    6. Looping
      1. Bounded Loops versus Unbounded Loops
      2. The while Loop
      3. Exercise 3.5: Printing the Numbers 1 to 10 Using a while Loop
      4. The do…while Loop
      5. Exercise 3.6: Converting a while Loop to a do...while Loop
      6. The for Loop
      7. Exercise 3.7: Using a for Loop to Print the Days of the Week
      8. The foreach Loop
      9. Exercise 3.8: Using a foreach Loop to Print the Days of the Week
      10. Nesting Loops
      11. Exercise 3.9: Using Nested foreach Loops
      12. The break Statement
      13. Exercise 3.10: Using a break Statement to Terminate a Loop's Execution
      14. The continue Statement
      15. Exercise 3.11: Using continue to Skip an Item in a List
      16. Alternative Control Syntaxes
      17. Using System Variables
      18. Activity 3.1: Creating a Movie Listing Script to Print Movies per Director
      19. Tips for Control Structures
    7. Summary
  5. 4. Functions
    1. Introduction
    2. What is a Callable?
      1. Exercise 4.1: Using Built-in Functions
      2. Types of Callables
    3. Language Constructs
    4. Introduction to Built-In Functions
      1. Finding Built-In Functions
      2. Parameters and Return Values
      3. Passing Parameters by Reference
      4. Passing Scalar Variables by Reference
      5. Optional Parameters
      6. Exercise 4.2: Working with print_r()
      7. A Varying Number of Parameters
      8. Flag Parameters
      9. Exercise 4.3: Using Built-In Functions with Arrays
    5. Introduction to User-Defined Functions
      1. Naming Functions
      2. Documenting Your Functions
      3. Namespaced Functions
      4. Pure Functions
      5. Scope
      6. The $GLOBALS Superglobal Array
      7. Exercise 4.4: Using the $GLOBALS array
      8. The Single Responsibility Principle
      9. The function Keyword
      10. Identifier
      11. Type Hints
      12. The Spread Operator (…) with Type Hints
      13. Parameters in User-Defined Functions
      14. Return Types in User-Defined Functions
      15. Signature
      16. Returning a Value
      17. Parameters and Arguments
      18. Optional Parameters
      19. Parameters Passed by Reference to Our Function
      20. Default Values for Parameters
      21. Exercise 4.5: Writing a Function that Adds Two Numbers
      22. Variable Functions
      23. Anonymous Functions
      24. Using a Variable from Outside of Scope Inside an Anonymous Function
      25. Exercise 4.6: Working with Anonymous Functions
      26. Exercise 4.7: Creating Variable Functions
      27. Exercise 4.8: Playing with Functions
      28. Activity 4.1: Creating a Calculator
    6. Summary
  6. 5. Object-Oriented Programming
    1. Introduction
    2. The Object-Oriented Approach
      1. OOP Concepts
      2. Classes
      3. Instantiating a Class
      4. Class Attributes
      5. Class Constants
      6. The $this Variable
      7. Class Methods
      8. Exercise 5.1: Using the Getter and Setter Methods
      9. Constructor
      10. Destructor
      11. Exercise 5.2: Instantiating the Class and Printing the Details
      12. Inheritance
      13. Exercise 5.3: Implementing Inheritance
      14. Access Modifiers
      15. Exercise 5.4: Applying Access Modifiers
      16. Static Fields and Methods
      17. parent:: and self::
      18. Exercise 5.5: Applying a Static Member
      19. Class Abstraction
      20. Exercise 5.6: Implementing an Abstract Class
    3. Interfaces
      1. Exercise 5.7: Implementing an Interface
      2. Abstract Classes versus Interfaces
      3. Class Type Hinting Plays a Role in Dependency Injection
    4. Overriding
      1. Attribute Overriding
      2. Method Overriding
      3. Exercise 5.8: Overriding an Inherited Method
      4. Overloading
      5. Attribute Overloading
      6. Method Overloading
      7. Exercise 5.9: Implementing Attribute and Method Overloading
      8. Final Classes and Methods
      9. Exercise 5.10: Implementing a Final Class and Methods
    5. Traits
      1. Exercise 5.11: Implementing Trait
      2. Class Autoloading
    6. Namespaces
      1. Exercise 5.12: Implementing Namespaces
      2. Activity 5.1: Building a Student and Professor Object Relationship
    7. Summary
  7. 6. Using HTTP
    1. Introduction
    2. The Request-Response Cycle of a Web Application
      1. A Typical HTTP Request
      2. A Typical HTTP Response
      3. Request Methods
      4. GET HTTP Requests
      5. POST HTTP Requests
    3. Query Strings
    4. PHP Superglobals
      1. $_SERVER
      2. Exercise 6.1: Dumping the $_SERVER Data
      3. $_COOKIE
      4. Exercise 6.2: Setting and Reading a Cookie
      5. $_SESSION
      6. Exercise 6.3: Writing and Reading Data from a Session
      7. $_GET
      8. Exercise 6.4: Using Query Strings in Web Pages
      9. $_POST
      10. Exercise 6.5: Sending and Reading POST Data
      11. $_FILES
      12. Exercise 6.6: Uploading a File and Validating its Type
      13. Securing Input and Output Data
      14. Best Practices
      15. Sanitizing and Validating the User Input
      16. Exercise 6.7: Sanitizing and Validating the User Input
      17. Escaping the Output
      18. Cross-Site Scripting (XSS)
      19. Exercise 6.8: Securing against XSS
    5. Cross-Site Request Forgery (CSRF)
      1. Exercise 6.9: Securing against CSRF
      2. Building an Application (Bootstrapping the Examples)
        1. web/
        2. src/
        3. components/
        4. handlers/
        5. templates/
      3. Exercise 6.10: Building an Application: The Home Page
      4. Exercise 6.11: Building an Application: The Profile Page and the Login Form
      5. Activity 6.1: Creating a Support Contact Form
    6. Summary
  8. 7. Data Persistence
    1. Introduction
    2. File I/O Handling
      1. Reading Files with PHP
      2. A Simple File Read
      3. Exercise 7.1: A Simple File Read (All at Once)
      4. Reading Files with the fread Function
      5. Exercise 7.2: Reading Files with the fread Function
      6. Benchmark File Reading
      7. Exercise 7.3: Benchmark File Reading
      8. Reading Files Line by Line
      9. Exercise 7.4: Reading Files Line by Line
      10. Reading CSV Files
      11. Exercise 7.5: Reading CSV Files
      12. Downloading a File with PHP
      13. Exercise 7.6: Downloading a File
      14. Writing a File with PHP
      15. Exercise 7.7: Writing to Files
      16. Exercise 7.8: Appending Content in Files
      17. Other Filesystem Functions
      18. Deleting a File with PHP
      19. Exercise 7.9: Deleting a File with PHP
      20. Moving Files with PHP
      21. Exercise 7.10: Creating Directories and Moving Files to the Archive
      22. Copying Files Using PHP
      23. Exercise 7.11: Copying Files
    3. Databases
      1. GUI Clients
      2. Connecting to MySQL
      3. Connecting to MySQL
      4. Exercise 7.12: Connecting to MySQL
      5. Creating a Database
      6. Exercise 7.13: Creating a Database
      7. Creating a Table
      8. Exercise 7.14: Creating the Table
      9. Inserting Data into a MySQL Database Table
      10. Exercise 7.15: Inserting Data into a Table
      11. SQL Injection
      12. Prepared Statements
      13. Using Prepared Statements
      14. Exercise 7.16: Inserting Data Using Prepared Statements
      15. Fetching Data from MySQL
      16. Exercise 7.17: Fetching Data from MySQL
      17. Updating Records in MySQL
      18. Exercise 7.18: Updating Records in MySQL
      19. Deleting Records from MySQL
      20. Exercise 7.19: Deleting Records from MySQL
      21. The Singleton Pattern
      22. Activity 7.1: Contact Management Application
    4. Summary
  9. 8. Error Handling
    1. Introduction
    2. Errors in PHP
      1. Handling Errors
      2. The Default Error Handler
      3. Using a Custom Error Handler
      4. Exercise 8.1: Using a Custom Error Handler
      5. Exercise 8.2: Logging with the Custom Error Handler
      6. Triggering a User-Level Error
      7. Exercise 8.3: Triggering Errors
      8. Logging Errors at Script Shutdown
      9. Exercise 8.4: Logging Fatal Errors at Shutdown
    3. Exceptions
      1. Basic Usage
      2. Exercise 8.5: Implementing Exceptions
      3. Custom Exceptions
      4. Exercise 8.6: Custom Exceptions
      5. Custom Exception Handler
      6. Using a Custom Exception Handler
      7. Exercise 8.7: Using a Custom Exception Handler
      8. Translating Errors to Exceptions
      9. Exercise 8.8: Translating Errors to Exceptions
      10. Exercise 8.9: Simple Exception Handling
      11. Exercise 8.10: Better Usage of Exceptions
      12. Activity 8.1: Handling System and User-Level Errors
    4. Summary
  10. 9. Composer
    1. Introduction
    2. Dependency Management
      1. Using Composer
      2. Exercise 9.1: Getting Started with Composer
      3. Initializing a Project
      4. Exercise 9.2: Initializing a Project
      5. Requiring Packages
      6. Exercise 9.3: Adding Dependencies
    3. Semantic Versioning
      1. Applying Version Constraints
      2. Exercise 9.4: Applying Version Constraints
      3. The Lock File
      4. Exercise 9.5: Re-Installing Vendor Files
      5. Dev Dependencies
      6. Exercise 9.6: Installing Development Dependencies
      7. Packagist
      8. Exercise 9.7: Discovering Packages on Packagist.org
    4. Namespaces
      1. Autoloading
      2. Using Composer Packages
      3. Exercise 9.8: Using PSR-4 to Load Classes
      4. Exercise 9.9: Implementing Monolog
      5. Activity 9.1: Implementing a Package to Generate a UUID
    5. Summary
  11. 10. Web Services
    1. Introduction
    2. An Example Web Service
      1. Selecting Third-Party APIs
      2. RESTful Concepts
      3. Request Formats
      4. Exercise 10.1: JSON Encoding
    3. HTTP Headers
      1. Authentication and Authorization
      2. Manual API Testing
      3. Exercise 10.2: Manual API Testing with Insomnia
      4. Making a Request with PHP
      5. Exercise 10.3: Making a GET Request with Guzzle
      6. Exercise 10.4: Sending a POST Request with Headers
      7. Activity 10.1: Making Your Own POST Request to httpbin.org
    4. Summary
  12. Appendix
    1. Chapter 1: Introducing PHP
      1. Activity 1.1: Displaying Query Strings in the Browser
    2. Chapter 2: Types and Operators
      1. Activity 2.1: Printing the BMI of a User
    3. Chapter 3: Control Statements
      1. Activity 3.1: Creating a Movie Listing Script to Print Movies per Director
    4. Chapter 4: Functions
      1. Activity 4.1: Creating a Calculator
    5. Chapter 5: Object-Oriented Programming
      1. Activity 5.1: Building a Student and Professor Object Relationship
    6. Chapter 6: Using HTTP
      1. Activity 6.1: Creating a Support Contact Form
    7. Chapter 7: Data Persistence
      1. Activity 7.1: Contact Management Application
    8. Chapter 8: Error Handling
      1. Activity 8.1: Improving the User Experience through the Handling System and User-Level Errors
    9. Chapter 9: Composer
      1. Activity 9.1: Implementing a Package to Generate a UUID
    10. Chapter 10: Web Services
      1. Activity 10.1: Making Your Own POST Request to httpbin.org
18.227.0.192