The PHP Practitioner

We all start somewhere. When it comes to web development with PHP, well, your first stop is this series. Designed specifically and exclusively for beginners, here, you'll learn the fundamentals of PHP - all the way down to defining variables and arrays.

If you feel somewhat unprepared for the content at Laracasts, this "PHP for beginners" series should be your next stop. New lessons are published every Thursday, so don't fall behind!

The PHP Practitioner

We all start somewhere. When it comes to web development with PHP, well, your first stop is this series. Designed specifically and exclusively for beginners, here, you'll learn the fundamentals of PHP - all the way down to defining variables and arrays.

If you feel somewhat unprepared for the content at Laracasts, this "PHP for beginners" series should be your next stop. New lessons are published every Thursday, so don't fall behind!

So you've decided to become a web developer? Excellent, excellent choice. At this point, you have a basic understanding of HTML and CSS, and are now ready to take the next step. Right? Great! Let's learn PHP from the ground up.

Step 1 Homework: Get PHP 7, the latest version at the time of this recording, installed on your machine using one of the methods outlined above. You might use Homebrew on the Mac, or possibly MAMP, WAMP, or XAMPP. Any of these options will do just fine. To confirm that you've completed the homework, open the command line, and run php -v. Does it say PHP 7? Congrats!

Before we get started, let's ensure that you have a proper code editor installed on your machine. As you'll find, code editors are much like fashion: everyone has their own preference. It doesn't matter which you choose, so pick one!

Step 2 Homework: Install a proper code editor on your machine right now. It could be Sublime Text, Atom, PHPStorm, or a different one entirely.

PHP is a language built for the web. As such, it mixes with regular HTML beautifully. Let's learn how that all works in this episode. You'll review embedding PHP in HTML, calling your first function, and fetching URL parameters.

Step 4 Homework: Echo a sentence onto the page, while fetching a URL parameters from the address bar.

We'll touch on a slightly higher level topic in this episode, but only lightly. If you stick with it long enough, you'll begin to hear the phrase "separation of concerns." At our current skill level, this means we should give consideration to the fact that, at the moment, our PHP logic and HTML presentation are a bit too intertwined. Perhaps we can separate these concerns.

Step 5 Homework: Play around with creating a new file, preparing a few variables, and then loading a separate "view" file to present the HTML.

Let's learn about a new construct: arrays. Think of them as primitive collections of related data. Maybe you have an array of names, or animals, or practically anything. Even better, we can filter through this data and act upon it with ease.

Step 6 Homework: Create an array of any five animals, and then loop over them with foreach, and display each one within a list item.

"Boolean" is a scary, but simple term to understand. It's a data type that represents one of two values: true or false. As you'll find, you'll reference booleans constantly within your projects. So it's important that you understand this one.

A conditional is how we branch our logic into different paths. "If some value is true, then we want to proceed in this way. Otherwise, let's handle it in that way." As you can see, booleans and conditionals go hand-in-hand.

Homework: Continue tinkering with conditionals. Add a new boolean to your task array, and use its value to branch off into two different paths within your HTML.

Think of a function as a container for reusable pieces of logic. In this lesson, you'll create your very first function.

Homework: Write a function that determines if a provided age (sent through as an argument) is old enough to enter a 21-and-up night club. Have this function return a boolean, which can be used in any conditional.

You've only just begun learning the fundamentals of PHP, and already I'm throwing something else at you. Sorry (bows head in shame), that's just how it is; there are lots of moving pieces to your typical web application. We must take some to review databases. This is how we "remember" information. Or, in other words, if a user creates a handful of todo items, how will we remember those todos for when they return to our site next week? Well, the answer is through a database.

I know you're excited about MySQL, and want to fetch records using PHP. However, we need to make one quick pit stop. We must learn the basics of writing classes, and creating objects. So let's tackle that in this episode, and then I promise we'll move on to writing SQL queries with PHP.

In this episode, you'll perform your first refactor. "Refactor" is a term we use to describe the process of changing the structure of your code, without affecting the underlying behavior. I'll be pushing your limits a bit in this episode, so you may need to watch this one twice. As always, ask any questions you mind have in the comments below.

At the moment, we're hardcoding our database username and password directly within the Connection class. But, we don't really want to do this. It's too dangerous. Instead, let's extract any secret passwords or keys for our app into its own file, and then lock that down.

Let's discuss routing in this episode. While, yes, you can create PHP files that correspond to the URI, this breaks down pretty quickly. So instead, we'll make a very basic router. This allows us to associate any URI endpoint with a controller.

Let's take some time to work on our views. At the moment, we've been rewriting the entire wrapping HTML for every single page. Naturally, this breaks down very quickly in real life. Each time you update, say, your navigation area, you'd need to open several different view files to make the change. No thanks. Instead, let's review the first step to drying up your templates.

PHP's various array functions are tools that absolutely need to rest in your belt. While the good ole'foreach can usually get the job done, more often than not, there's a better way. In this episode, I'll introduce you to array_filter, array_map, and array_column.

Let's transition over to working with forms. If we want a form's submission to POST to a particular endpoint, well, our router isn't yet set up to handle that. How exactly might we associate a URI with the request type (GET, POST) that it should respond to? In this episode, we'll do a bit of refactoring for this very thing!

So far, we've only written the logic to fetch records from the database. But now it seems that we need to, not only insert new records, but do so dynamically. It sounds like we'll need to update our QueryBuilder class to allow for this.

No more PHP requires! They're annoying to write, aren't they? Instead, let's take some time to switch over to Composer, which is an industry standard for not just dependency management, but also autoloading.

"Dependency injection Container" sounds like a super scary thing. But it's easy to understand! Think of them as boxes. Apply a label, and throw your stuff into the box. When you need them back, simply look for the corresponding label! In this episode, we'll transition from our simplistic $app array, to a dedicated DI container.

So far in this series, our "controllers" have simply been plain PHP files. In this episode, however, we'll refactor the router to allow for loading and calling actions on a controller class. I think you'll find that this drastically simplifies our workflow.

At the moment, all of our classes share the same global namespace. But, think about it: you wouldn't throw your entire music collection into a single folder, and the same is true for your PHP classes. In this lesson, we'll switch over to dedicated namespaces, while discussing the benefits to doing so.

It's time for you to meet my framework of choice: Laravel (duh). In this lesson, we'll review the basic folder structure, discuss how it is similar to the custom-built framework from this series, and then review why it's preferred, in this author's opinion, to choose a battle-tested solution like Laravel over manually wiring things together.