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.
Publisher resources
Table of contents
-
Preface
-
About the Book
- About the Chapters
- Conventions
- Before You Begin
- Installing PHP 7.3 (Ubuntu)
- Installing PHP 7.3 (Mac OS X)
- Installing PHP 7.3 (Windows)
- Installing MySQL 5.7 (Ubuntu)
- Accessing MySQL as Root (Using sudo)
- Creating a Test User
- Granting all Privileges on a Test User
- Installing MySQL Workbench on Ubuntu
- Installing MySQL 5.7 (Mac OS)
- Installing MySQL Workbench on Mac OS
- Installing MySQL 5.7 (Windows)
- Installing Composer
- Installing the Insomnia REST Client
- Installing the Code Bundle
-
About the Book
-
1. Introducing PHP
- Introduction
-
Getting Started with PHP Web Development
- Built-in Templating Engine
- PHP in the Interactive Shell
- Exercise 1.1: Printing Hello World to the Standard Output
- Exercise 1.2: Printing Hello World by Executing a PHP File
- Assigning and Using Variables
- Creating and Assigning Variables to Print Simple Messages on the Web Browser
- Exercise 1.3: Using Input Variables to Print Simple Strings
- Exercise 1.4: Using the Built-in Server to Print a String
- HyperText Markup Language
- Cascading Style Sheets
-
Using the Server Variable
- Exercise 1.7: Displaying Server Information
- Other Predefined Variables
- Assignment by Value and by Reference
- Exercise 1.8: Assigning a Variable by Reference and Changing its Value
- Using isset to Check for Variable Declaration
- Exercise 1.9: Using isset to Check whether a Variable Has Been Set
- Activity 1.1: Displaying Query Strings in the Browser
- Summary
- 2. Types and Operators
-
3. Control Statements
- Introduction
- Boolean Expressions
- Boolean Constants
- Logical Operators
-
Branching
- The if Statement
- The if…else Statement
- Exercise 3.1: Creating a Basic Script to Implement the if...else Test Case
- Exercise 3.2: Implementing the Nested if...else Structure
- The Ternary Operator
- The if…elseif…else Statement
- Exercise 3.3: Creating a Script Using the if... elseif... else Statement
- The switch Case
- Exercise 3.4: Creating a Script to Implement a Switch Case
-
Looping
- Bounded Loops versus Unbounded Loops
- The while Loop
- Exercise 3.5: Printing the Numbers 1 to 10 Using a while Loop
- The do…while Loop
- Exercise 3.6: Converting a while Loop to a do...while Loop
- The for Loop
- Exercise 3.7: Using a for Loop to Print the Days of the Week
- The foreach Loop
- Exercise 3.8: Using a foreach Loop to Print the Days of the Week
- Nesting Loops
- Exercise 3.9: Using Nested foreach Loops
- The break Statement
- Exercise 3.10: Using a break Statement to Terminate a Loop's Execution
- The continue Statement
- Exercise 3.11: Using continue to Skip an Item in a List
- Alternative Control Syntaxes
- Using System Variables
- Activity 3.1: Creating a Movie Listing Script to Print Movies per Director
- Tips for Control Structures
- Summary
-
4. Functions
- Introduction
- What is a Callable?
- Language Constructs
- Introduction to Built-In Functions
-
Introduction to User-Defined Functions
- Naming Functions
- Documenting Your Functions
- Namespaced Functions
- Pure Functions
- Scope
- The $GLOBALS Superglobal Array
- Exercise 4.4: Using the $GLOBALS array
- The Single Responsibility Principle
- The function Keyword
- Identifier
- Type Hints
- The Spread Operator (…) with Type Hints
- Parameters in User-Defined Functions
- Return Types in User-Defined Functions
- Signature
- Returning a Value
- Parameters and Arguments
- Optional Parameters
- Parameters Passed by Reference to Our Function
- Default Values for Parameters
- Exercise 4.5: Writing a Function that Adds Two Numbers
- Variable Functions
- Anonymous Functions
- Using a Variable from Outside of Scope Inside an Anonymous Function
- Exercise 4.6: Working with Anonymous Functions
- Exercise 4.7: Creating Variable Functions
- Exercise 4.8: Playing with Functions
- Activity 4.1: Creating a Calculator
- Summary
-
5. Object-Oriented Programming
- Introduction
-
The Object-Oriented Approach
- OOP Concepts
- Classes
- Instantiating a Class
- Class Attributes
- Class Constants
- The $this Variable
- Class Methods
- Exercise 5.1: Using the Getter and Setter Methods
- Constructor
- Destructor
- Exercise 5.2: Instantiating the Class and Printing the Details
- Inheritance
- Exercise 5.3: Implementing Inheritance
- Access Modifiers
- Exercise 5.4: Applying Access Modifiers
- Static Fields and Methods
- parent:: and self::
- Exercise 5.5: Applying a Static Member
- Class Abstraction
- Exercise 5.6: Implementing an Abstract Class
- Interfaces
- Overriding
- Traits
- Namespaces
- Summary
-
6. Using HTTP
- Introduction
- The Request-Response Cycle of a Web Application
- Query Strings
-
PHP Superglobals
- $_SERVER
- Exercise 6.1: Dumping the $_SERVER Data
- $_COOKIE
- Exercise 6.2: Setting and Reading a Cookie
- $_SESSION
- Exercise 6.3: Writing and Reading Data from a Session
- $_GET
- Exercise 6.4: Using Query Strings in Web Pages
- $_POST
- Exercise 6.5: Sending and Reading POST Data
- $_FILES
- Exercise 6.6: Uploading a File and Validating its Type
- Securing Input and Output Data
- Best Practices
- Sanitizing and Validating the User Input
- Exercise 6.7: Sanitizing and Validating the User Input
- Escaping the Output
- Cross-Site Scripting (XSS)
- Exercise 6.8: Securing against XSS
- Cross-Site Request Forgery (CSRF)
- Summary
-
7. Data Persistence
- Introduction
-
File I/O Handling
- Reading Files with PHP
- A Simple File Read
- Exercise 7.1: A Simple File Read (All at Once)
- Reading Files with the fread Function
- Exercise 7.2: Reading Files with the fread Function
- Benchmark File Reading
- Exercise 7.3: Benchmark File Reading
- Reading Files Line by Line
- Exercise 7.4: Reading Files Line by Line
- Reading CSV Files
- Exercise 7.5: Reading CSV Files
- Downloading a File with PHP
- Exercise 7.6: Downloading a File
- Writing a File with PHP
- Exercise 7.7: Writing to Files
- Exercise 7.8: Appending Content in Files
- Other Filesystem Functions
- Deleting a File with PHP
- Exercise 7.9: Deleting a File with PHP
- Moving Files with PHP
- Exercise 7.10: Creating Directories and Moving Files to the Archive
- Copying Files Using PHP
- Exercise 7.11: Copying Files
-
Databases
- GUI Clients
- Connecting to MySQL
- Connecting to MySQL
- Exercise 7.12: Connecting to MySQL
- Creating a Database
- Exercise 7.13: Creating a Database
- Creating a Table
- Exercise 7.14: Creating the Table
- Inserting Data into a MySQL Database Table
- Exercise 7.15: Inserting Data into a Table
- SQL Injection
- Prepared Statements
- Using Prepared Statements
- Exercise 7.16: Inserting Data Using Prepared Statements
- Fetching Data from MySQL
- Exercise 7.17: Fetching Data from MySQL
- Updating Records in MySQL
- Exercise 7.18: Updating Records in MySQL
- Deleting Records from MySQL
- Exercise 7.19: Deleting Records from MySQL
- The Singleton Pattern
- Activity 7.1: Contact Management Application
- Summary
-
8. Error Handling
- Introduction
-
Errors in PHP
- Handling Errors
- The Default Error Handler
- Using a Custom Error Handler
- Exercise 8.1: Using a Custom Error Handler
- Exercise 8.2: Logging with the Custom Error Handler
- Triggering a User-Level Error
- Exercise 8.3: Triggering Errors
- Logging Errors at Script Shutdown
- Exercise 8.4: Logging Fatal Errors at Shutdown
-
Exceptions
- Basic Usage
- Exercise 8.5: Implementing Exceptions
- Custom Exceptions
- Exercise 8.6: Custom Exceptions
- Custom Exception Handler
- Using a Custom Exception Handler
- Exercise 8.7: Using a Custom Exception Handler
- Translating Errors to Exceptions
- Exercise 8.8: Translating Errors to Exceptions
- Exercise 8.9: Simple Exception Handling
- Exercise 8.10: Better Usage of Exceptions
- Activity 8.1: Handling System and User-Level Errors
- Summary
- 9. Composer
- 10. Web Services
- Appendix
Product information
- Title: The PHP Workshop
- Author(s):
- Release date: October 2019
- Publisher(s): Packt Publishing
- ISBN: 9781838648916
You might also like
book
Moodle 4 E-Learning Course Development - Fifth Edition
Leverage Moodle 4.0's improved UX features and new tools to create effective courses in today's competitive …
book
Migrating to AWS: A Manager's Guide
Bring agility, cost savings, and a competitive edge to your business by migrating your IT infrastructure …
book
The Art of WebAssembly
WebAssembly is the revolutionary new web standard that enables high-performance programs and computer-intensive web applications to …
book
Web-Based Projects that Rock the Class: Build Fully-Functional Web Apps and Learn Through Doing
Explore the modern concepts of client-server web applications. This book includes examples that are simple to …