PHP For Beginners
We all start somewhere. For programming, this series is that first step. I've designed it specifically for newcomers to, not just PHP, but programming in general. Here, you'll learn the fundamentals of PHP - all the way down to defining basic variables and arrays.
Think of this series as a key stepping stone for your programming journey.
Progress
Series Info
- Episodes
- 50
- Run Time
- 10h 44m
- Difficulty
- Beginner
- Last Updated
- Oct 9, 2023
- Version
- PHP 8
Series Episodes
- The Fundamentals (11)
How to Choose a Programming Language
How exactly do you choose a first programming to learn? Why PHP, and not Ruby? And what about JavaScript or Python? How can you possibly be expected to make an educated decision at this early stage of your learning?Tools of the Trade
Before we can dig in, we must first create the world, so to speak. To follow along with this series, you'll need to install a handful of essential tools. Let's go over them in this episode.Your First PHP Tag
Our first order of business is to prepare some basic HTML, boot a PHP server, and view it in the browser.Variables
Okay, let's move on and review basic concatenation and variables. The first time I learned about variables, my first thought was, "But why?". Let's talk about it!Conditionals and Booleans
For this next episode, we'll build a quick webpage that displays a dynamic message based upon whether or not you've read a particular book. This will give us the opportunity to review both conditionals and booleans.Arrays
At this point, you know how to create a variable for a primitive string or number. But what about situations when we want to declare a collection, or list of things? A list of usernames, or book titles, or tweets? In these situations, we can reach for arrays.Associative Arrays
Let's stick with arrays just a little longer. In this episode, you'll learn the syntax for accessing individual items within an array. You'll also learn about associative arrays, which allow you to associate a key with each value.Functions and Filters
Congratulations for making it this far! Let's take things a step further now and review functions. You can think of functions as the verbs of the programming world.Lambda Functions
Buckle up, because we have a lot to cover in this episode! As part of our first code refactor, we'll discuss what lambda functions are, as well as when and why you might reach for them.Separate Logic From the Template
Before we move on to the technical check-in for this chapter, let's set aside a bit of time to discuss code organization and why we might want to separate our PHP logic from the view, or HTML.Technical Check-in #1 (With Exam)
Before we move on to section two, let's do a quick technical check-in to ensure that everything you've learned so far has been committed to memory. And don't forget to complete the quiz before continuing.
- Dynamic Web Applications (9)
Page Links
Welcome to section two! While section one focused entirely on the initial fundamentals of PHP, now, we'll move on and learn what it looks like to build a typical PHP application with a MySQL database.PHP Partials
In the previous episode, we begrudgingly duplicated the same HTML for every PHP view. Let's fix that now by reaching for PHP partials.Superglobals and Current Page Styling
Often, you'll need to apply styles or trigger certain logic based upon the current page. Luckily, we can use PHP's$_SERVERsuperglobal array to dynamically determine the current page.Make a PHP Router
I think your PHP skills have now matured to the point that you're ready to build a custom PHP router from scratch. This will give us the chance to discuss code organization, response codes, and more.Create a MySQL Database
We're now ready to begin interacting with a MySQL database. Before we write any PHP, let's first review how to connect to MySQL, and then create a database and table.PDO First Steps
The next step on our journey is to figure out how to connect to MySQL from PHP and execute a simple SELECT query. We'll of course reach for PHP Data Objects, or PDO, to orchestrate this task securely.Extract a PHP Database Class
Now that we understand the basic logic for initializing a PDO instance and executing a prepared query, let's clean things up a bit by extracting a dedicatedDatabaseclass.Environments and Configuration Flexibility
We have one glaring issue with ourDatabaseclass right now. The connection values have been hard-coded. So what happens when we push the project to production, where the host and port are entirely different?SQL Injection Vulnerabilities Explained
In this episode, we'll review some examples of SQL injection and discuss why it's so important that you always assume that, on the web, a person is guilty until proven innocent.
- Notes Mini-Project (8)
Database Tables and Indexes
You've learned enough of the fundamentals at this point to begin working on your first mini-project. Let's make a notes app! We'll begin with the initial database structure, which will give us the opportunity to review MySQL indexes.Render the Notes and Note Page
Now that we have the initial database structure in place, let's create one page to display all of John Doe's notes, and then another page for each individual note.Introduction to Authorization
In the previous episode, we added support for reading all notes that were created by a particular user. But, in doing so, we unwittingly introduced a major security concern. In this lesson, we'll discuss authorization, status codes, and magic numbers.Programming is Rewriting
Before we move on to building a form to persist new notes to the database, let's take ten minutes to refactor our current code and discuss wrapping up APIs you don't own.Intro to Forms and Request Methods
We're overdue, but it's finally time to dig into forms. In this lesson, we'll learn how to submit a form using two different request methods. Next, we'll discuss how our controller might detect whether a POST submission has occurred.Always Escape Untrusted Input
In this lesson, we'll finally persist a new note to the database. But, in doing so, you'll be introduced to a new security concern that requires us to always escape user-provided input.Intro to Form Validation
In this lesson, we'll review two layers of form validation: browser and server-side. We can use validation to ensure and confirm that the user submits their data in the exact format that we require.Extract a Simple Validator Class
To make for a more flexible and readable experience, let's extract the basic validation rules we wrote in the previous episode into a dedicated Validator class.
- Project Organization (8)
Resourceful Naming Conventions
Let's take a short pause on the notes exercise, and instead switch our attention to general code organization. We'll start by switching to a common naming convention for resources.PHP Autoloading and Extraction
Okay, buckle up. This will be the most dense episode yet, as we discuss a variety of topics related to project organization. We'll touch on document roots, helper functions, constants, PHP autoloading, and more.Namespacing: What, Why, How?
It's time to discuss PHP namespacing, but don't worry: I'm going to make this incredibly easy to understand. If you can remember the days of storing your entire music collection locally, you'll grasp namespacing in seconds.Handle Multiple Request Methods From a Controller Action?
Over the next three episodes, we'll review a number of refactors that are a bit more advanced. But first, we need to encounter a situation that necessitates the refactors. We'll use the example of a messy controller action that can respond to multiple request types.Build a Better Router
In this episode, we'll build a better router that can handle and respond to any form request type. However, because forms only natively support GET and POST, we'll need to use a hidden input field to spoof the request type.One Request, One Controller
With an improved router that can respond to multiple form request types, we can now refactor our controller actions to be more focused. We'll also discuss the importance of keeping your controller actions as simple as possible.Make Your First Service Container
In this episode, we'll discuss the concept of a service container, and how it can help us organize our code and remove the need to manually construct the same dependencies over and over again.Updating With PATCH Requests
The only remaining step for our first resource is to support editing notes. Luckily, most of the legwork has been completed at this point. We only need to create the form, and register a corresponding route and controller.
- Sessions and Authentication (5)
PHP Sessions 101
Welcome to a new chapter! We'll now begin to explore the world of PHP sessions. We'll start by discussing the concept of sessions, and how they can be used to persist data across multiple requests.Register a New User
Now that we understand the basics of PHP session handling, we can build our first registration form. Once filled, we can create a new user in the database and then update the session to "mark" them as signed in.Introduction to Middleware
In this episode, we'll discuss the concept of middleware, and how it can be used as a bridge between an incoming request and the core of your application. We'll create two middleware classes to handle route authorization for guests and authenticated users.Manage Passwords Like This For The Remainder of Your Career
Up until this point, we've been storing passwords in clear text (gasp)! Let's fix that. In this episode, we'll discuss the importance of password hashing, and why it's necessary to protect your users' sensitive data. We'll also discuss the Bcrypt algorithm, and how it can be used to generate secure hashes.Log In and Log Out
Now that we have a functioning registration form, we can finally build out the login system. As you'll see, there's a small handful of confusing, low-level steps that we need to follow - particularly when logging a user out.
- Refactoring Techniques (5)
Extract a Form Validation Object
For our first refactoring exercise, we'll extract a login form validation object. This approach will allow us to keep our controllers lean, clear, and easier to potentially reuse in other places.Extract an Authenticator Class
Next up, we can further clean up our controller and improve clarity by extracting anAuthenticatorclass. This new class can then be responsible for handling the actual authentication logic.The PRG Pattern (and Session Flashing)
In this episode, we'll discuss the PRG (Post-Redirect-Get) pattern, and how it can be used to prevent duplicate form submissions. Currently, we're returning a view directly from thePOSTrequest in the event of failed validation, however, this is far from ideal. Let's review the problem, and then seek a solution.Flash Old Form Data to the Session
Now that we understand how to temporarily "flash" data to the session, we can now return to the topic of displaying "old" form data to the user.Automatically Redirect Back Upon Failed Validation
Next up, we'll learn how to automatically redirect back to the form upon failed validation. If we can manage to get this to work, we'll remove a significant amount of duplication and clutter from our various controllers.
- Meet Composer (2)
Composer and Free Autoloading
I think it's time to move on to a new chapter and finally discuss Composer. Composer is a world-class dependency manager that allows us to declare any and all libraries or packages that our project requires. Then, with a single command, we can install them in seconds. Even better, it ships with an excellent autoloader.Install Two Composer Packages: Collections and PestPHP
Now that we've learned how to install Composer, itself, the next step is to pull in some packages! For demonstration purposes, we'll install two separate packages.
- Testing (1)
- Wrapping Up (1)
