PHP 8 for Absolute Beginners: Basic Website and Web Application Development 1484282043, 9781484282045

Embark on a practical journey of building dynamic sites aided by multiple projects that can be easily adapted to real-wo

186 33 10MB

English Pages 452 [439] Year 2022

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Table of Contents
About the Authors
About the Technical Reviewer
Introduction
Part: I
Chapter 1: Getting Ready to Program
Objectives
Setting Up a Development Environment
What Is PHP? How Does PHP Work?
Apache and What It Does
Storing Info with MySQL/MariaDB
Installing PHP, Apache, and MySQL/MariaDB
Installing XAMPP
Step 1: Download XAMPP
Step 2: Follow the Instructions
Step 3: Test XAMPP to Ensure Proper Installation
Open the XAMPP Control Panel
What If Apache Isn’t Running?
Verify That Apache and PHP Are Running
Choosing a PHP Editor
Creating Your First PHP Program
Running Your First PHP Script
Summary
Projects
Chapter 2: Understanding PHP: Language Basics
Objectives
Embedding PHP Scripts
Program Design and Logic
Program Design and Logic
Using echo
What Is a Variable?
Storing Values in a Variable
A Variable Is a Placeholder
Valid PHP Variable Names
Program Design and Logic
Displaying PHP Errors
Secure Programming
Creating an HTML5 Page with PHP
HTML Review
Including a Simple Page Template
Including the Template
Secure Programming
Commenting Your Code
Block and Single-Line Comments
Avoiding Naming Conflicts
The Object Operator
Using a StdClass Object for Page Data
Page Views
HTML Review
Making a Dynamic Site Navigation
HTML Review
Passing Information with PHP
Accessing URL Variables
Using isset( ) to Test If a Variable Is Set
Secure Programming
$_GET, a Superglobal Array
Including Page Views Dynamically
Strict Naming Convention
Displaying a Default Page
Securing the Program
Validating Your HTML
Styling the Site with CSS
CSS Review
Declaring a Page_Data Class
Program Design and Logic
Classes Make Objects
Highlighting Current Navigation Item with a Dynamic Style Rule
Summary
Projects
Chapter 3: Form Management
Objectives
What Are Forms?
Setting Up a New PHP Project
Seeing for Yourself
Creating a Dynamic Navigation
Creating Page Views for the Form
Displaying Page Views on the Index Page
Program Design and Logic
A Simple Search Form
The Element and Some Common Types
Understanding the Method Attribute
Named PHP Functions
Program Design and Logic
The Basic Syntax for Named Functions
Program Design and Logic
Program Design and Logic
Using Function Arguments for Increased Flexibility
Creating a Form for the Quiz
HTML Review
Showing the Quiz Form
The POST Method
Secure Programming
Using the $_POST Superglobal
Program Design and Logic
$_POST Is an Array
Secure Programming
Curly’s Law: Do One Thing
Program Design and Logic
Code Is Poetry
OOP: Using Constructors, Getters, and Setters
Secure Programming
Summary
Exercises
Chapter 4: Building a Dynamic Image Gallery
Objectives
Setting Up a Dynamic Site
Prerequisites: A Folder with Some Images
Copyright Laws
Creating a Navigation
Creating Two Dummy Page View Files
Creating the Index File
Time to Test
Preparing a Function for Displaying Images
Iteration
While Loop
For Loop
Using glob to Find Files in a Folder
For Each Loop
Showing All Images
Secure Programming
CSS Review
Creating a Form View
Showing a Form for Uploading Images
php.ini
$_FILES
Secure Programming
Uploading Files with PHP
Planning an Uploader Class
UML
Uploader Class Requirements
The Magic Method __construct()
Saving the Uploaded File
Using the Uploader Class
The Single Responsibility Principle
Summary
Projects
Chapter 5: Reviewing PHP 8 Basic Syntax
Objectives
From the Beginning
Comments
PHP Functions
Variables
Conditional Statements
Logical Operators
Functions
Arrays
Loops
Enums
Summary
Projects
Part: II
Chapter 6: Databases, MVC, and Data Objects
Objectives
The Basics of MySQL/MariaDB Data Storage
Manipulating Data with SQL
Developing a Database for the Poll
Building a Database Using CREATE
The CREATE TABLE Statement
Secure Programming
Understanding PRIMARY KEY
Understanding AUTO_INCREMENT
The INSERT Statement
The SELECT Statement
Secure Programming
The UPDATE Statement
Secure Programming
Coding a Database-Driven Site Poll
Separating Concerns with MVC
Planning the Logic
Creating the Poll Project
Making a Poll Controller
Making a Poll Model
Making a Poll View
Hooking Up Poll View with Poll Model
Connecting to MySQL/MariaDB from PHP
PHP Data Objects (PDO)
Opening a Connection
Sharing the Database Connection with the Poll Model
Retrieving Data with a PDOStatement
Showing a Poll Form
Updating a Database Table According to Form Input
Secure Programming
Responding to User Input
Summary
Projects
Chapter 7: Building the Basic Blog System
Objectives
Creating the blog_entry Database Table
Planning the PHP Scripts
Admin View: Creating the Admin Blog Site
Creating the Admin Entry Manager Navigation
Loading Admin Module Controllers
Creating the Admin Entry Input Form
Styling the Admin Editor
Connecting to the Database
Using Design Patterns
The Table Data Gateway Design Pattern
Writing the Entry_Table Class
Secure Programming
Processing the Admin Form Input and Saving the Entry
User View: Getting Data for All Blog Entries
Using an SQL SUBSTRING Clause
Using an SQL Alias
Preparing a User View for All Blog Entries
Hooking Up the User View and User Model
Responding to User Requests to Read More
Getting Entry Data
Secure Programming
Creating a Blog View
Displaying an Entry
Code Smell: Duplicate Code
Staying DRY with Curly
Refactoring with Curly
Secure Programming
Summary
Projects
Chapter 8: Basic Blog: Entries and Comments
Objectives
Creating a Model for the Administrative Module
Displaying Administrative Links
Populating the Form with the Entry to Be Edited
Handling Entry Deletion
Deleting Entries from the Database
Responding to Delete Requests
Preparing a Model to Update Entries in the Database
Controller: Should I Insert or Update?
Secure Programming
Insisting on a Title
Secure Programming
User View: Building and Displaying the Comment Entry Form
A Combined View
Creating a Comment Table in the Database
Using a Foreign Key
Building a Comment_Table Class
Inheritance
Is-a Relationships
Using Inheritance in Our Code
Creating a View for Listing Comments
Hooking Up View and Model to Display Comments
Inserting a Comment Through the Comment Form
Searching for Entries
The Search View
Responding to a User Search
The Search Model
Searching with a LIKE Condition
A Search Result View
Loading a Search Result View from the Controller
Exercise: Improving Search
Summary
Projects
Chapter 9: Basic Blog: Images and Authentication
Objectives
Deleting Entries in Related Tables
Understanding Foreign Key Constraints
Deleting Comments Before Blog Entry
Creating an Image Manager
Showing a Form for Uploading Images
A Quick Refresher on the $_FILES Superglobal Array
Uploading an Image
Exercises
Displaying Images
Using an Image in a Blog Entry
Improving Security with Authentication
Creating an admin_table in the Database
Hashing the Password with BCRYPT
One-Way Hashing
Sufficient Security
Adding Administrators in the Database
Building an HTML Form
Saving New Administrators in the Database
Planning Login
Creating a Login Form
Hiding Controls from Unauthorized Users
HTTP Is Stateless
Superglobal: $_SESSION
Persisting State with a Session
Logging Users Out
Allowing Authorized Users Only
Exercises
Summary
Projects
Chapter 10: Data Dashboard and Gaming
Objectives
Setting Up a Data Dashboard
Gathering Microsoft Excel, CSV, JSON, and Database Data
Creating the Model Data Class
Creating the Drop-Down and File Type Views
Creating the Front Door Controller and the Subcontrollers
Creating the Logic for a Checkers Game
Summary
Projects
Index

PHP 8 for Absolute Beginners: Basic Website and Web Application Development
 1484282043, 9781484282045

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers