The PHP Workshop

Book description

Get to grips with the fundamentals of PHP programming and learn to build dynamic, testable PHP web applications with the help of real-world examples and hands-on projects

Key Features

  • Start building modern and testable PHP web applications
  • Master the basic syntax and fundamental features of PHP
  • Implement object-oriented programming to write modular, well-structured code

Book Description

Do you want to build your own websites, but have never really been confident enough to turn your ideas into real projects? If your web development skills are a bit rusty, or if you've simply never programmed before, The PHP Workshop will show you how to build dynamic websites using PHP with the help of engaging examples and challenging activities.

This PHP tutorial starts with an introduction to PHP, getting you set up with a productive development environment. You will write, execute, and troubleshoot your first PHP script using a built-in templating engine and server. Next, you'll learn about variables and data types, and see how conditions and loops help control the flow of a PHP program. Progressing through the chapters, you'll use HTTP methods to turn your PHP scripts into web apps, persist data by connecting to an external database, handle application errors, and improve functionality by using third-party packages.

By the end of this Workshop, you'll be well-versed in web application development, and have the knowledge and skills to creatively tackle your own ambitious projects with PHP.

What you will learn

  • Set up a development environment and write your first PHP scripts
  • Use inheritance, encapsulation, polymorphism and other OOP concepts
  • Use HTTP and understand the request-response cycle of an application
  • Perform file operations and interact with external databases
  • Deal with application errors and handle exceptions
  • Use third-party libraries and manage dependencies
  • Connect your application to web services to allow for data exchange

Who this book is for

This book on PHP for beginners will help you if you're just getting started with PHP. Although prior programming experience is not necessary, a basic understanding of HTML, CSS, and JavaScript will help you grasp the concepts covered more easily.

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 (Mac OS X)
      6. Installing PHP 7.3 (Windows)
      7. Installing MySQL 5.7 (Ubuntu)
      8. Accessing MySQL as Root (Using sudo)
      9. Creating a Test User
      10. Granting all Privileges on a Test User
      11. Installing MySQL Workbench on Ubuntu
      12. Installing MySQL 5.7 (Mac OS)
      13. Installing MySQL Workbench on Mac OS
      14. Installing MySQL 5.7 (Windows)
      15. Installing Composer
      16. Installing the Insomnia REST Client
      17. 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. 1. Introducing PHP
      1. Activity 1.1: Displaying Query Strings in the Browser
    2. 2. Types and Operators
      1. Activity 2.1: Printing the BMI of a User
    3. 3. Control Statements
      1. Activity 3.1: Creating a Movie Listing Script to Print Movies per Director
    4. 4. Functions
      1. Activity 4.1: Creating a Calculator
    5. 5. Object-Oriented Programming
      1. Activity 5.1: Building a Student and Professor Object Relationship
    6. 6. Using HTTP
      1. Activity 6.1: Creating a Support Contact Form
    7. 7. Data Persistence
      1. Activity 7.1: Contact Management Application
    8. 8. Error Handling
      1. Activity 8.1: Improving the User Experience through the Handling System and User-Level Errors
    9. 9. Composer
      1. Activity 9.1: Implementing a Package to Generate a UUID
    10. 10. Web Services
      1. Activity 10.1: Making Your Own POST Request to httpbin.org

Product information

  • Title: The PHP Workshop
  • Author(s): Alexandru Busuioc, David Carr, Markus Gray, Vijay Joshi, Mark McCollum, Bart McLeod, M A Hossain Tonu
  • Release date: October 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838648916