PHP is a scripting language that helps WordPress’ different elements talk to the server. This lets it access databases, makes your site dynamic, and almost runs the show. Without PHP, we wouldn’t have the modern web as we know it.
Over the course of this post, we’re going to answer the question: What is PHP? You’ll understand the benefits of using it, how it works with WordPress, and how your site needs to be compatible with a current version of the language. To finish off, we’ll also talk about some alternatives to PHP, that may factor into WordPress’ future code base.
A Brief Primer on How the Web Works (And the Programming Languages It Uses)
For the unaware, most website files live on a web server. This lets you pull and push files in an optimal way, and has an infrastructure that’s different to a regular computer. However, there are two elements to a site that need different approaches with regards to the data it provides:
- Client-side. For this post’s purpose, the client is your browser (although it could be your computer if we talk about Secure File Transfer Protocol (SFTP)).
- Server-side. The web server fulfills this role, and in a basic sense is a single web server . However, you could have multiple servers for handling different tasks with your database.
When it comes to the holy trinity web languages, these are all client-side. They handle the following:
- HyperText Markup Language (HTML). Consider this the structural language for your site. If you take a look at our HTML5 cheat sheet, you’ll see that it lets you define ‘containers’ and divisions for your site.
- Cascading StyleSheets (CSS). If HTML is the structure for your site, CSS is the design language. You can add all sorts of colors, typography, and shapes to your site. It’s accurate to say that almost everything you see on a web page uses CSS to showcase it.
However, because there is also server-side functionality to consider, and most websites use databases, you also need another language to help the client- and server-sides to talk. Enter PHP.
What PHP Is (and How It Works)
PHP is a server-side scripting language that acts as a fundamental part of the web. It runs on the web server, and lets whatever you do on the front end known to it. Because of this, it has an important job to carry out.
In fact, the only time you’ll see PHP code on the front end is due to an error: either through ‘miscoding’, or a genuine issue with your site.
Of course, a website will also have a database too, and PHP is just as vital for accessing this and returning the data as part of the request. While it’s not the only server-side scripting language available (and we’ll discuss this later), it has a massive market share at nearly 80 percent.
What Scripting Can Do for a Site
At this point, it should be clear how PHP (and scripting in general) can help a site, but you may not understand exactly what it can do. While you’ll see some real-world examples in a later section, we can give you a few snippets to ponder:
- It’s the go-to way to generate dynamic content on a website, such as form data.
- You can use scripting to handle cookie information.
- Scripting will manage and work with files and folders on the web server.
- Along with Structured Query Language (SQL) in most cases, your database will need to use scripting to add, remove, and modify its data too.
As such, a scripting language is an essential component – one of the ‘fab four’ – and when it comes to PHP, it’s the leading language around.
Why Most of the Web Uses PHP
Because almost 80 percent of the web uses PHP, the logical conclusion is that there’s something special about it. In fact, there are just as many benefits of using PHP as there are for scripting in general:
- PHP is an open-source language. This means it’s free to download and use as you see fit.
- There are hundreds – if not thousands – of tutorials on how to learn PHP, and there’s a shallow learning curve.
- Because the language is so popular, there’s tons of support from almost every host available.
- PHP also offers built-in support for MySQL databases, which makes it a top choice for many ‘back-end developers’.
- The update and maintenance schedule is regular. You’ll often see new versions come out, with support from your host. For example, PHP 7.3 and 7.4 are popular, while PHP 8 offers vast improvements for your site, but doesn’t yet have full adoption.
To touch on this last point a little more, a database can come in various ‘flavors’, and WordPress will throw an error if you don’t have a database at all. MySQL is popular, but MariaDB is a complete open-source fork that offers total compatibility.
However, PHP does have its detractors who state that the code is confusing on a fundamental level, and that other modern coding languages can do a greater job. Even so, WordPress uses PHP, so for that reason alone it’s a good idea to understand more about how the platform and language work.
How WordPress Uses PHP Under the Hood
WordPress doesn’t use PHP in a different way to other applications, per se. However, it does need some explaining. If you head to the WordPress.org site, you can download the full version of the platform’s core files:
If you extract the ZIP file and take a look at the contents, you’ll notice that most end with the .php extension:
Every file that wants to use the scripting language, it will need that extension. However, you’ll also see HTML in there too, and we’ll show you an example of this later. The themes and plugins that WordPress uses also use the same sort of files.
Within your site’s files, there will also be plenty of ‘functions’, ‘hooks’ (both ‘actions’ and ‘filters’), and more that helps WordPress use the PHP language in a safe and typical way. You might even use them within your functions.php file to enhance the platform’s feature set.
These snippets can be compact and succinct. For example, the two-word
the_content(); function will display the entire post or page content. As such, there’s a lot of power within PHP to achieve almost anything you need on your site. It’s also why WordPress can be such a flexible Content Management System (CMS).
A Real-World Example of PHP Used In WordPress
To show you how you use PHP within WordPress, we can open a file and take a look. We’re going to use the 404.php file, because it’s short and simple to understand:
<header class="page-header alignwide">
<h1 class="page-title"><?php esc_html_e( 'Nothing here', 'twentytwentyone' ); ?></h1>
</header><!-- .page-header -->
<div class="error-404 not-found default-max-width">
<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentytwentyone' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .page-content -->
</div><!-- .error-404 -->
We’ve taken out some of the commenting for brevity, but the core code here is enough to work with. You’ll notice that the first three lines declare that the code is PHP (
<?php ?>). In fact, these tags need to bookend every piece of PHP code, as you’ll see.
The file first calls a function to get the site’s header, then after the closing tag, there’s a clump of HTML code. This shows some of the flexibility (and complexity) of how the different languages work together. You can write HTML and PHP in the same file, but you have to be strict with regards to each language. HTML doesn’t require the opening and closing tags.
Throughout that HTML, you’ll also notice one or two lines of PHP. This is how dynamic content gets into WordPress. Snippets will pull data from your server and database, and display it wherever you note within your HTML. You define the structure with HTML, and display the data with PHP.
At the end of the file, you again open a new PHP tag, and tell the page to display the footer content. This is a simplistic overview, but it does show how many moving parts WordPress has. It also shows why you might encounter errors based on how many plugins or themes you install, especially if they are poor examples without developer maintenance.
Alternatives to PHP (And Why WordPress Doesn’t Use Them)
Although PHP commands 80 percent of the market when it comes to server-side scripting, it’s not the only solution you’ll hear of. In fact, there are a number of other languages that a developer could use. For example:
- Ruby. This is as old as PHP, but isn’t a dedicated web development language. Even so, it sees some use as part of the developer ‘stack’ as Ruby on Rails, and has a large fanbase.
- ASP.NET. This is Microsoft’s take on server-side scripting. It has a ten percent market share, and finds use as a scripting language for medium and large scale operations. It has an arguable higher quality than PHP, but costs more to develop.
- Python. You’ll likely know Python as a general-purpose language that’s popular in data science. Even so, coupled with a framework such as Django or Flask, Python is a fantastic server-side language with a simple structure.
You’ll find that these languages combined only make up around 15-20 or so percent of all sites. PHP offers a blend of low development costs, a shallow learning curve, and a high level of support. As for why WordPress uses PHP, these factors play a part.
However, it’s fair to say that convention is also a factor. The original fork of WordPress (b2/cafelog) used PHP, so WordPress did the same. It’s not a bad choice, and in fact lets WordPress offer all of the elements we take for granted today.
WordPress has much more going on under the hood than most users realize. In fact, there’s a programming language working to help the CMS’ files work with the web server and database. PHP is a core language for web development, and it’s also central to how WordPress works.
This post has looked to answer the question: What is PHP? Throughout, we’ve talked about what it can do, how scripting in general works, and even an example of PHP code from WordPress’ files. While there are other scripting languages too with lots of positives, PHP is a market leader and offers almost unparalleled power for every WordPress website.
Did this article help you understand what PHP is, and do you have any further questions? Ask away in the comments section below!