What Is PHP? A Beginners’ Guide to WordPress’ Core Scripting Language – WPKube

As we talk about a lot on the WPKube blog, WordPress has a lot of moving parts. As such, it also uses a number of different programming languages and scripts to make everything talk and work together. While you’ll often read of the ‘holy trinity’ languages – HTML, CSS, and JavaScript – there’s also a fourth language involved that you’ll have heard of: ‘PHP: Hypertext Preprocessor’ (PHP). If you read the term and still ask, What is PHP?, we can help!

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.

Inside this article

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:

When it comes to the holy trinity web languages, these are all client-side. They handle the following:

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.

For example, consider the simple task of navigating to a website. You’ll type the URL into the browser, and when you confirm the action, PHP code will run on the server and return the relevant HTML, CSS, and JavaScript. While you can often see the client-side languages and markup within a browser, it’s rare to see PHP.

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:

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:

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.

PHP supports both of these, but also lets you connect to any sort of database type, such as PostgreSQL and even new modern examples such as MongoDB.

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:

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:

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.

Wrapping Up

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!

This content was originally published here.