PHP for Beginners: Starting on Backend WordPress Development

learn php wordpress

WordPress, the content management system the internet loves. You can use it for years without needing to tackle PHP, but eventually you’re finding yourself needing it. You go to Bing and search “php for beginners” and you find yourself here. The journey to learn PHP for WordPress development is long, but let’s start!

We’ll kick off this WordPress coding tutorial with a little summary of PHP’s role in WordPress, and then start to build up from there.

Using PHP in WordPress: Useful for Everyone, Necessary for Developers

You don’t really need to ever write PHP code as a WordPress user, WordPress business owner, or other similar role. A minority of the people who use WordPress on a daily basis even know what PHP is, never mind know how to write code in it. But WordPress developers, WordPress developers must use PHP. But I’m getting ahead of myself…

WordPress Runs atop PHP on the Server

Before we jump fully into our PHP for beginners tutorial, some background:

Web servers, it turns out, are just computers. And those computers need to have underlying layers they can run on. For most WordPress sites, that breaks down to be:

  • Linux (the operating system, like Mac OS or Windows),
  • Apache (the web server, thing your browser talks to),
  • MySQL (the database, where posts live),
  • and PHP (which coordinates with the database, OS, and files to build web pages).

I wrote a lot more about this in our “WordPress LAMP” article:

A WordPress LAMP?! An Introduction to WordPress Infrastructure

PHP is a Programming Language

So, hopefully the above made you aware that PHP is something that WordPress uses under the hood. It’s a programming language, and the language that WordPress server-side code is written in. (In the web browser of both administers and visitors, WordPress often also involves languages called HTML, CSS, and JavaScript).

PHP was one of the first and most popular languages that people used to build HTML documents (aka “web pages.”) Its popularity is a little more complex than I want to cover here, but I did write a “Why PHP” article over on Thoughtful Code for those who are interested in that.

In short: PHP is a logic-programming language which you can use to control which HTML a page shows, either in WordPress or outside of it. The files that make up both WordPress themes and plugins are mostly using PHP to build the pages that you see when you visit a WordPress site in your web browser.

Learn PHP for WordPress and You’ll be Able to Modify Themes, Make Plugins

As we just covered, both WordPress plugins and themes use a lot of PHP. (Though in 2022, the amount of PHP you’ll see in themes has just gone down…) Essentially, everything in a plugin is enabled by the PHP code you write. For a WordPress theme, some functionality is coming from WordPress PHP and some will be in HTML you write into your theme template files. You’ll generally need less PHP expertise to make good themes than good plugins for WordPress, but it’s an important skill in either case.

While we won’t get into this much in this introductory PHP WordPress tutorial, for those wondering, the basic way that WordPress plugins work is with WordPress hooks: actions and filters. If you already understand what PHP function, variable, and strings are, you can jump right into that with this guide:

WordPress Hooks, Actions, and Filters: What They Do and How They Work

A Beginner’s PHP Tutorial for WordPress

Alright, now that we’ve got that WordPress stuff out of the way, we can start in earnest on our short PHP programming for beginners tutorial. We’ll focus on a few core things: what PHP looks like, what things you must understand to make any sense of PHP, and what next steps make sense.

PHP 101: Where We Start on a PHP Tutorial for Beginners

So, PHP as we mentioned above started as a way to create more dynamic HTML. As such, you’ll know you’re writing PHP, and not HTML, in a .php file because it’ll be boxed in by what are most commonly called “PHP tags.” Those PHP tags are things that fence off PHP from your HTML, and vice-versa. Although there is still some interaction.

Here’s an example:

<!-- file.php -->
<html>
<?php echo 'Hi from PHP'; ?>
</html>

What would loading file.php up from your web server show you in your web browser? It’ll show the words, “Hi from PHP”. (The word echo in PHP essentially lets something exit PHP-interaction-land and show on the page. What’s more, if you viewed the page source in that browser, you’d also see that the <html> opening and closing tags come through. Where not controlled, all HTML from a PHP file just shows in your browser.

Last note: that first line, which starts <!-- is an HTML comment. Comments are lines in code which shouldn’t do anything, but may help you or another programmer make sense of the program later. In PHP, most comments are broken out with to forward slashes, // comment here, or fenced with /* */ characters, like this:

<?php
/*
Nothing in these lines
 will show or do anything
*/
echo 'Not a comment'; // What's to the left will run, but this text itself won't
// Neither will this
?>

You’ll also want to note that our echo line ends with a semicolon. All lines of PHP will generally end with a { (of which more later) or a semicolon ;. Statements like echo should always end with a semicolon. This is a kind-of-strange convention across many programming languages.

Variables, Integers, and Strings, Oh My!

We just showed your first PHP data type: the string. A “string” is a common programming-language term for a sequence of characters. In our specific case above, our string was the sequence of characters “Hi from PHP.” In PHP, a string can be differentiated from other words (the ones that are just the program itself) via either single-quotes or double-quotes. So both of these are equal:

<?php 
$val = "Hi WPShout Reader!";
$val = 'Hi WPShout Reader!';
echo $val;

What will the above do? It’ll show “Hi WPShout Reader!” in your web browser. How? Through the use of a variable. What’s a variable?! Great question. A variable is a stored value. This term is common across most programming languages including PHP. Variables can be many things: strings, integers (i.e. 1, 2, 99, 12931298), floating-point numbers (0.123, 4.39, 6.928348723074082370974092384), things called arrays or objects, and whole lot more.

In PHP, all variable names will start with a dollar sign ($). They’ll then have at least one character after that dollar sign, which is unique. If you, as we do above, set (using the equal sign) a variable twice (or more), it will always have the later-set value. You can use this to “vary” the value of a variable. (The above code does not.)

You’ll also note that in the last code sample we didn’t have a “closing PHP tag” ?>. This is because those aren’t strictly necessary at the end of a .php file. They’re sometimes used, but if you’re not going back to “HTML land”, it’s a best practice that you don’t use closing PHP tags at the end of files.

Functions: Where You Start to Learn PHP for WordPress Development

Another thing we must cover to complete our little PHP 101, WordPress PHP tutorial, is functions. Functions in PHP are essentially another name for “stored procedures.” A stored procedure can be something as simple as an adder, which adds two value together, or a “show-er” function that just shows a specific string.

A basic add function in PHP would be defined like this:

function add($first_number, $second_number) {
    return $first_number + $second_number;
}
$value = add(1, 3); // $value is set to 4
echo $value; // show $value in the HTML/browser

The word function tells PHP that the next thing it should see will be the name of the function. Then there are parenthesis with what are called “arguments” or “parameters.” A function can have no parameters, which would look like function no_arguments() {}.

Finally, most functions will return something, though they don’t have to. In PHP, a function returns one (1) thing. In our case, our function returns a number. It’s important to know that a return value isn’t shown in your browser by default. Instead it can be shown with echo, saved to a variable like $value, of have further operations performed on it.

That’s why in the code above HTML wouldn’t show anything from our PHP code until we did our echo $value, which shows the value stored in the $value variable, which for this specific code will be 4.

Basic Conditionals (If, Then, Else)

Another thing you’ll get a lot of value from when learning PHP for WordPress is coming to terms with conditional statements. If you’re able to live a day in the modern world, you understand conditional logic. And one of the fun and surprising things about learning PHP for most beginners in the pleasant shock of how “normal” conditional logic in PHP (and many other languages) is like normal thinking. The syntax is a bit like this:

$value = 5;
if ($value > 5) {
    execute_function();
} else {
    echo 'Too small';
}

In the above PHP code, which will happen?

  1. The parameter-less execute_function function is called and executed, or
  2. the string 'Too small' will be printed into the HTML?

If you guessed that the function will be called (#1), you may have forgotten that > is a “greater than” sign, and that 5 is not “greater than” 5. I think about the above code as saying “If $value is bigger than five, call function execute_function. Otherwise, echo 'Too small'“.

endif, endwhile and other Conditional Syntaxes

You should also note that in the above code sample our logic was bound by “curly braces,” {}. This is common in PHP, and many other languages. These curly brace are a unique and useful delimiter of the difference between different conditions.

In PHP, because you’re often mixing it in with HTML, you’ll sometimes logically have a dangling closing curly brace <?php } ?>. These are annoyingly hard to understand. So especially in HTML-heavy situations (like classic WordPress theme template files), many people like to use a more verbose kind of condition. So the following lines have the exact same outcome as what we saw above:

$value = 5;
if ($value > 5):
    execute_function();
else: ?>
    Too small
<?php endif; ?>

There are a few big changes here. The most important one is that we’ve switched from curly braces to colons : and an endif;. These work the same, but one makes the closing of which tag quite clear. Dangling curly braces are a bad time, especially when you’ve shifted from HTML to PHP and back again. This has always been common in classic WordPress theme files, because of what are called WordPress conditional tags.

But before we get there: the other change we made is that we changed echo 'Too small'; into just the literal words “Too small”, outside of PHP tags. echoing from PHP code is the same as simply putting a value outside of PHP tags into a file. The same PHP logic applies in those scenarios. So in this case, “Too small” would still only show when $value was less than or equal to 5.

A Little More About PHP and HTML for WordPress Server-Side Development (especially themes 🤓)

The big reasons to understand PHP tags like endif is that you’ll see them in the PHP files of WordPress themes a lot. Here’s a small snippet of one:

<header class="entry-header">
    <?php
    if ( is_singular() ) :
        the_title( '<h1 class="entry-title">', '</h1>' );
    else :
        the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
    endif;
    ?>
</header><!-- .entry-header -->

There’s a lot you don’t need to understand here, but I want to help you understand a few parts of it. It’s a strange mix on HTML, like <header>, and PHP.

The biggest thing to learn from it: the fact that PHP tags end and begin within the document (the Twenty Nineteen theme’s template-parts/content/content.php file), and that a “WordPress conditional tag” function called is_singular is called to change the markup of a function which shows the post title in WordPress. The title-showing function of WordPress is called “the title”, and its arguments here are the_title()‘s specific markup (for before and after what you entered in its “Title” field). This code contains HTML in PHP strings (a common thing in WordPress), and uses a “post template tag” and a “WordPress conditional tag.” It’s a lot of what you’ll see a lot in WordPress.

PHP Programming for Beginners: Where to Go Next

I’ve hinted at and pointed to a few other things relevant here so far. But we’ve got a lot of useful resources here on WPShout to help you get a better grip on the (copious and complex) material we’ve just covered. Here are some more pages I’d like you to look at, and why I think they’re valuable:

It’s a Long But Valuable Journey You’ve Begun: To Learn PHP for WordPress

If this was your first PHP tutorial, congratulations for making it through! I’m sure that you’re feeling a little lost. I covered a lot quite quickly, and if your brain is like mine it all felt a little… abstract. That’s the first part that’s hard about learning PHP for beginners.

I think the most important thing to do if you’re trying to learn PHP for WordPress development is try some WordPress coding yourself. There’s simply no substitute for going into your own WordPress site, knocking around some PHP tags, breaking some things, and hitting your head upon a challenge or 12 yourself. The things you learn when they solve your problem are so much more memorable than me (or other writers) telling your eyes that information 100 times.

Using PHP in WordPress is a thing that takes a lot of practice and work. But it also makes your more valuable to possible clients, to yourself in maintaining your own WordPress sites, and it makes it possible for you to help with WordPress itself. Not that you’ll go there right away. This was “PHP for absolute beginners” after all. But I think you’re well-set to get moving and learning PHP for WordPress. I hope you’ll find WPShout continually valuable in your effort to learn PHP for WordPress development. Cheers! 🙂

Image credit: Ben Ramsey


9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
jerry john
June 24, 2019 3:31 am

Thanks for sharing this. I’ve been trying to learn a little PHP. Great post for me and people who already use other languages like asp or asp.net and want to explore PHP. This is very helpful. Keep on the great work. I just want to say that there is no need to dive too deep in PHP if you don’t want to. A versatile coder can just pick what they need for a particular scenario.

Louis
February 14, 2019 4:41 am

“So in this case, “Too small” would still only show when $value was less than 5.” is wrong.

Correct would be:

“So in this case, “Too small” would still only show when $value was not greater than 5.

Freethinker
January 13, 2019 3:21 am

Personally, I think learn basic css and html is easier than js or php. But this article is clear and really helps.

Mary G
January 9, 2019 1:27 pm

Well, for me, as a beginner, this was not a 101 class. I guess I need a PHP primer class! (maybe I am pre-beginner) Like, what is the difference between HTML and PHP? (I thought only HTML was used in the text portion of websites.)

Mary G
January 10, 2019 11:33 am
Reply to  David Hayes

Well, kinda. I still think I need to go to pre-basics!

Amie Suzan
January 8, 2019 11:33 am

Nice article indeed. Would definitely be helpful for the beginners.

Jason Robie
January 8, 2019 8:29 am

As always. Well written and on-point!
This kind of article is what got me excited about “Up and Running”!
Keep up the good work, David!