Let’s start at the beginning of the beginning: what is PHP?
Here’s the simplest and most helpful definition: PHP is a programming language that outputs HTML.
Let’s break that statement down into its two parts:
A Programming Language…
The difference is [control flow]. Control flow is what gives us the ability to do things like execute one statement if another statement turns out to be true, to define a statement in one place and execute it later, or to execute a statement over and over again as many times as we define.
- Alter the contents of a page based on the role of the currently logged-in user.
- Count the new comments on a post from the past week, and output the count as a live-updating “## Recent User Comments” badge at the top of that post’s comment section.
- Add a “Happy Monday!” banner to the top of your site—but only if it’s Monday.
Why can’t you build these features with HTML and CSS? Because these features rely on things causing other things. That is control flow, and that’s what a programming language gives you that a declarative language like HTML and CSS doesn’t. You can use declarative languages to say “An <h1> goes here, and its font-size is 30px,” but for full-on programming as in the examples above, you need a programming language.
“Things causing other things” is control flow, and that’s what a programming language gives you that a declarative language doesn’t.
Again, we’ll get into the details later on. But for now, when we say PHP is a programming language, we simply mean that, rather than being limited in the manner of a declarative language, it’s a full-featured language that you can use to create just about anything—using the wonders of control flow.
…That Outputs HTML
Let’s take just about the simplest line of PHP code you can write:
<?php echo '<h1>Hello world!</h1>'; ?>
This line will print HTML to the page. In fact, it turns exactly into the following line of HTML code:
Don’t worry about understanding how PHP does this or how the PHP code above works, which we’ll learn in the next few chapters. For now, focus on the fact that PHP’s core job in this example is to print HTML to the page. PHP is, in essence, a more powerful way to write HTML than doing it by hand.
PHP is, in essence, a more powerful way to write HTML than doing it by hand.
We can see this super-clearly in what the acronym “PHP” stands for.
What PHP Stands For
PHP was written by nerds, so the acronym does something cool or annoying, depending on your perspective. Here’s what PHP stands for:
This is what’s known as a recursive acronym, because the first “P” stands for “PHP,” and the first “P” in that stands for “PHP,” and so on. Whether you think this is very cool or very stupid—or you’re just confused—let’s move on from the first P.
Hypertext means HTML. That’s what the H in HTML stands for. So PHP is closely related to HTML. How?
Preprocessor means that PHP does processing—calculations, like a programming language does—before the HTML itself is interpreted. What does this mean? It means that PHP runs through its processes, crunching and calculating, and ultimately yielding HTML, just like we saw in the code example above.
At the end of PHP’s processes, you’re left with pure HTML—just like if you’d written the HTML yourself by hand. This is good, because HTML happens to be what web browsers can read.
In other words, PHP is a chance to program the HTML we output. HTML is not a programming language, so they invented PHP in order to bring the benefits of programming languages (like [[[if-statements]]], [[[loops]]], and lots else) into the job of outputting HTML.
PHP is a chance to program the HTML we output.
Have another look at our first code example above, and imagine how
But Isn’t PHP So Much More Than That?
Well, football (soccer) is ultimately about a ball crossing a white painted goal line. But it’s so much more than that. PHP is similar.
In your day-to-day work as a WordPress developer, most of what you use PHP for will ultimately be delivered—as HTML—to the user’s web browser. This is as true if you’re doing a complex query to the WordPress database (so that you can output the correct content the user requested) as if you’re writing a complex membership plugin (so that non-logged-in users don’t see the HTML of members-only pages). And so on.
In your day-to-day work as a WordPress developer, most of what you use PHP for will ultimately be delivered—as HTML—to the user’s web browser.
There are exceptions to this rule. An example of one would be a PHP-based backup plugin that is designed to output a .zip file to your desktop. But football also has juggling contests that have nothing to do with a goal—and this is just the first chapter, so let’s not get ahead of ourselves. For the most part, preprocessing—“doing programming language stuff”—and then outputting HTML is what PHP is for, and having that understanding in mind will help you throughout this resource.