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!
Now that you're ready to begin coding, let's start at the top. What exactly is a variable, and why would you ever use or need one?
Step 3 Homework: Create a variable, and figure out how to echo it onto the screen or page.
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.
We've learned about basic arrays, but you may also creative associative arrays when necessary. The only difference is that each value within the array is associated with a key. Let me show you.
Homework: Create an associative array for a
task. This array should include such details as the title of the task, its due date, who it's assigned to, and whether it has been completed.
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.
As for MySQL GUIs, you might consider any of the following:
Connectionclass. 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.
View the source for this episode on GitHub.
foreachcan usually get the job done, more often than not, there's a better way. In this episode, I'll introduce you to
POSTto 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.
View the source code for this episode on GitHub.
$apparray, to a dedicated DI container.
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.