Learn the fundamentals of PHP in this free PHP course. Follow along to learn PHP and use it to write web apps.
Follow Along, Learn by Doing
I encourage you to follow along and to do exactly what I do on screen. This way you learn by doing.
To help, the PHP course GitHub repo contains the source for each exercise and sample project that was built throughout the course.
To get follow along, you’ll need a copy of MAMP (macOS) or XAMPP (for Windows and Linux). Download them here:
What You Will Learn in This Free PHP Course
In this course you’ll learn the most important skills writing apps for the web.
- variables and syntax
- how to make your applications more intelligent by making decisions from within your code
- arrays and the looping over those arrays
- how to write your own functions
- how PHP treats the variables both inside and outside of those functions
- how to get and handle user input from both HTTP GET and POST request
- parsing JSON
Later you will learn how to build a web based glossary application.
You will also learn:
- how to organize your project using controllers and FUSE
- how to read from and write to files
- how to build a simple authentication system
- how to use a MySQL database
PHP is a server-side language. This means it runs on a server instead of browser. Secondly PHP is used for server-side development. So the first thing you need to do is set up an environment for PHP development.
What does this mean? We first have to set up our computer to run as a web server. And this is how it will work: We make a request to a web server, the web server executes the PHP and then it returns the resulting HTML.
In this section you will learn what tools you need to set up your PHP environment. You will also learn by doing. So stop the video as many times as you want as you follow along and set a PHP environment on your computer.
2. The PHP Programming Language
Syntax and Variables
Every language has its own syntax. In this lesson, you’ll learn the PHP basics, and I’ll show you how to store data in memory by using PHP variables.
Error Reporting
Error messages are important to developers—they tell us why and where something went wrong. By default, we won’t see any PHP errors in the browser, so in this lesson we’ll modify our PHP configuration to display error information in a PHP error log.
PHP Conditions and Decisions
Almost every application you write will need to make decisions and respond to certain conditions. We’ll look at PHP conditions and decisions in this part of our PHP tutorial.
Some things you’ll be introduced to include:
- what is a condition in programming?
- conditional operators
-
if
andelse
statements
PHP Arrays
Arrays are an integral part of any programming language. These are data structures that allow us to store multiple values within a single variable. In this lesson, we’ll look at traditional PHP arrays as well as PHP associative arrays.
- what is an array?
- how to create an array
- associative arrays
PHP Loops
In this lesson we’ll look at loops. Loops and arrays go hand in hand because loops allow us to loop over an array and output each item within that array. So if we wanted to easily build a list of my favorite items, we can do that very easily with a loop.
PHP Functions
Functions are one of the most important parts of any programming language. They help us avoid code duplication by allowing us to run the same set of instructions over and over again on different data. In fact, throughout this entire course we have used functions in every lesson.
You’ll learn:
- basic concepts of functions in PHP
- how to define your own functions in PHP
- returning values from a function and function parameters in PHP
- setting default argument values or passing an argument by reference
Local and Global PHP Variables
We use variables to store data in memory. Simple, but there are some rules that you need to understand. We’ll discuss those rules in this lesson, and we’ll also look at a couple of different PHP variable types: PHP local variables and PHP global variables. Also, I’ll explain default function parameters.
Learn about:
- scope
- what is a local variable
- what is a global variable
PHP Include and PHP Require
Applications require a lot of code, and we need to organize our application into different files to keep things manageable.
In this lesson, we’ll look at how to use multiple files with PHP include and PHP require.
3. User Input and Saving Data in PHP
PHP Get Requests
Web apps are different from traditional applications because users input data through HTTP requests. In this part of our PHP tutorial, I’ll show you how to retrieve data from HTTP GET requests.
PHP Post Requests
We use PHP POST requests to submit sensitive information or to change the state of our application. In this lesson, I’ll show you how to use $_POST
to get data from a POST request.
We sometimes need to persist data throughout the lifespan of our application. Sessions make that possible, so we’ll write a simple (but functional) authentication system using PHP sessions.
4. The Web Glossary Project
In the previous lessons we’ve focused on individual parts of PHP so you can learn how it works. Now it’s time to put that theory to practice. We’ll get hands on and build a fun learning project—a glossary of web terminology.
Separating PHP Logic From Presentation
A well organized project is easy to develop. In this lesson we’ll learn the organization of a project. We’ll focus on separating concerns. This is a concept that is not just used in PHP, or web development, but development in general. The idea is that you have different components within your application that are responsible for doing one thing, and one thing only.
Working With Files
Sometimes you need to read and write to files on the server computer. In this lesson, I’ll show you some of the functions we use to do that.
Parse JSON and Output Data
JSON has become an integral part of any web application. Every language now has built-in JSON capability including PHP.
In this lesson, you’ll learn:
- how to change a string into JSON
- how you can work with JSON by parsing it into arrays and objects that we can then use in our application
Adding a Detail Page
Our web glossary is coming along. We’re displaying a list of terms, but we need a page to display the details of individual terms. We want users to have the ability to click on a term and be taken to their definitions (for example). That’s what we’ll build in this lesson.
Truthiness and Adding a PHP Search
Searching is a common feature many apps require, so in this lesson we’ll implement a PHP search feature for our web glossary app. Along the way, I’ll teach you about truthiness and falsiness in PHP.
Managing PHP Paths
Paths, especially relative paths, can be hard to work with in PHP apps. In this lesson, I’ll show you how to find the path to the root of your application. Along the way, you’ll get comfortable working with PHP paths as well as PHP relative paths.
Creating PHP Terms
Editing something is a lot like creating something… with some differences. In the previous lesson, you learned how to create PHP terms, and in this lesson, you’ll learn how to edit PHP terms. We’ll create the view and write the code for editing and saving data in our web glossary app.
Editing PHP Terms
Editing something is a lot like creating something… with some differences. In the previous lesson, you learned how to create PHP terms, and in this lesson, you’ll learn how to edit PHP terms. We’ll create the view and write the code for editing and saving data in our web glossary app.
Deleting PHP Terms
Sometimes you just have to delete some data. In this lesson, I’ll show you one of the ways we can handle deleting PHP terms and deleting data in our application.
5. Object-Oriented Programming
Intro to PHP Classes
Classes are a way to organize our code and make it easier to understand. In this lesson you will learn the basics of PHP’s classes. We’ll start by creating an object.
Creating a Data Provider Class
We can use classes to group and encapsulate related functionality. In this lesson, we’ll convert our file functions into a FileDataProvider
class. a PHP class. So we’ll reorganize our file code, and make adjustments to our application to use the provider pattern in object-oriented programming.
Writing a Data Abstraction Layer
In this lesson, we’ll write a data abstraction layer (DAL) to sit between our application and our DataProvider
. This will give us even more flexibility while protecting our data access code.
Inheritance
Object-oriented programming is all about code reuse, and we can reuse functionality for similar classes with inheritance. You’ll learn how in this lesson.
6. PHP and MySQL
Intro to MySQL
When it comes to data storage, not much can match the speed and power of a database. In this lesson, I’ll give you a brief overview of MySQL and phpMyAdmin. We’ll also create our database.
Connecting to the Database
In this lesson, you’ll learn how to connect to a database with PDO, and we’ll start writing our MySqlDataProvider
class. We’ll talk about creating and reading data.
By the end of this lesson you will understand how you use a database and more importantly how you do four things with a database: create, read, update, and delete—CRUD for short.
Inserting Data
We need some data to work with. We can easily create sample data with phpMyAdmin, but it’s more fun if we write the code to insert data into the database.
Reading Data With SELECT
Now that we have data in the database, we need to read it so that we can display it in the browser. You’ll learn the basic SELECT
statement in this lesson.
We have learned how to use insert, select, update, and delete commands. You will now learn how to use them within PHP. You will learn PDO (PHP Data Objects) and how to use PDO to issue queries in order to start getting data from the database.
Using Prepared Statements
We need to get in the habit of using prepared statements when mixing SQL with external data. It will protect us from opening a huge security hole for SQL injection attacks in our application. Use them. Always.
Searching Data
Searching for information in a database is actually easy to do. It’s not Google-level searching, but it’s useful in most cases. I’ll show you how in this lesson.
Editing and Deleting Data
Some of the easiest things we can do with a database are editing and deleting records. You’ll learn how to do this with the UPDATE
and DELETE
commands.
Refactoring the Code
Database code is repetitive. In this lesson, we’ll clean up the code so it’s a little less repetitive.
7. New Features in PHP 8
Using Named Arguments
PHP 8 offers a new feature that lets you add clarity and flexibility to your function calls. We’ll look at named arguments in this lesson.
Promoting Class Properties
The new property promotion feature in PHP 8 makes it easier to create and assign values to a class’s properties. You’ll learn how to use it in this lesson.
Simplifying String Functions
It’s taken 20+ years, but PHP 8 finally introduces simplified functions for testing strings and substrings. I may have an opinion or two on the subject…
The following tutorials will help you learn more PHP string tricks.
Conclusion
We covered a lot in this PHP tutorial: from PHP variables and syntax to PHP arrays, loops, functions, and scope. You also learned how to retrieve and handle user input from PHP GET and POST requests, as well as how to use the session to persist data between different pages. Not only that, but we implemented a real web app using these simple PHP skills!