PHP guide: Basic and simple PHP that every WordPress user should know

php code

If we want to grow in our mastery of WordPress, it is not enough just to learn how to use the block editor, not even to become a master of Divi or Elementor.

To really master WordPress, or any web creation tool, we must learn at least the basics of the components of the tool that, in the case of WordPress, we could summarize in:

  • HTML
  • CSS
  • PHP

It is true that it is not necessary to know these programming languages to create websites with WordPress, but if we add them to our arsenal our limit will be the sky, there will be nothing we can not do on a website, and our potential and confidence to address web projects will be unlimited.

We already saw some time ago the basic HTML and basic CSS that every WordPress user should know, so today we are going to get into the base that makes everything move, the basic PHP that will make us really understand WordPress and its operation, as well as allowing us to enter the world of programming.

I hope that this guide on basic and simple PHP will encourage you to learn even more to fully master WordPress, this fantastic tool that is what drives more than 40% of the entire Web.

As always, let’s start at the beginning…

What is PHP

PHP is something that WordPress uses in order to function. It is a programming language, and the language in which the WordPress server-side code is written. (In the web browser for both administrators and visitors, WordPress usually also uses languages called HTML, CSS and JavaScript).

PHP was one of the first and most popular languages that people have used to create HTML documents (also known as “web pages”).

In short: PHP is a logic programming language that can be used to control the HTML that displays a page, either in WordPress or outside of it.

The files that make up both WordPress themes and plugins mostly use PHP to build the pages you see when you visit a WordPress site in your web browser.

Learn PHP for WordPress and you will be able to modify and create themes and plugins.

Both WordPress plugins and themes use a lot of PHP.

Essentially, everything a plugin does is thanks to PHP code. For a WordPress theme, some functionality comes from the WordPress PHP and some will be in the PHP you write in your theme template files.

Generally, you’ll need less PHP expertise to make good themes than good WordPress plugins, but it’s an important skill either way.

While we won’t go into this too much in this basic WordPress PHP tutorial, for those who want to know, the basic way WordPress plugins work is with WordPress hooks: actions and filters.

Where to start in a PHP tutorial for beginners?

PHP started out as a way to create more dynamic HTML.

As such, you will know that you are writing PHP, and not HTML, in a .php file because it will be surrounded by what are commonly called “PHP tags”.

These PHP tags are elements that separate PHP from your HTML, and vice versa. However, there is always some interaction.

Here is an example:

<!-- file.php --> <html> <?php echo 'Hello, this is PHP'; ?> </html>
Code language: HTML, XML (xml)

What would loading file.php from your web server show in your browser? It would display the words, “Hello, this is PHP.” (The word “echo” in PHP essentially allows something to come out of PHP territory and show up on the page.)

What’s more, if you view the source code of the page in that browser, you’ll also see the opening and closing <html> tags displayed. When uncontrolled, the entire HTML of a PHP file is displayed in the browser.

Last note: That first line, which begins <!-- is an HTML comment. Comments are lines of code that shouldn’t do anything but may help you or another programmer understand the program in the future.

In PHP, most comments are separated with slashes, // Comment here, or with /* */ characters, as here:

<?php /* None of these lines will do or show anything */ echo 'This is not a commentary'; // The part on the left will execute but not THIS text // This one will not either ?>
Code language: HTML, XML (xml)

You’ll also want to note that our echo line ends with a semicolon. All PHP lines generally end with a “{" (which I’ll talk about later) or a semicolon “;" .

Statements like echo should always end with a semicolon. This is a somewhat unusual convention in many programming languages.

Variables, integrators and chains

We have just shown 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 character sequence “Hello, this is PHP”.

In PHP, a string can be differentiated from other words (those that are just the program itself) by single quotes or double quotes.

Both are the same:

<?php $val = "Hello, WPhelp reader"; $val = 'Hello, WPhelp reader'; echo $val;
Code language: HTML, XML (xml)

What will the above do? It will display “Hello, WPhelp reader” in your web browser.

How? By using a variable. What is a variable? Great question. A variable is a stored value.

This term is common in most programming languages, including PHP.

Variables can be many things: strings, integers (for example, 1, 2, 99, 12931298), floating point numbers (0.123, 4.39, 6.928348723074082370974092384), things called arrays or objects, and much more.

In PHP, all variable names start with a dollar sign ($). Then they will have at least one character after that dollar sign, which is unique.

If you, as we do above, set (using the “=” sign) a variable twice (or more), it will always have the value set later. You can use this to “vary” the value of a variable. (The code above does not do this).

You will also notice that in the last code example we did not have a “PHP closing tag” ?>. This is because these are not strictly necessary at the end of a .php file. Sometimes they are used, but if you are not going back to “HTML territory”, it is good practice not to use PHP closing tags at the end of files.

Functions: Where we all start learning PHP for WordPress development.

Another thing we need to cover to complete our little PHP for WordPress tutorial is functions.

Functions in PHP are essentially another name for “stored processes”. A stored process can be something as simple as a sum, which adds two values together, or a “display” function that just displays a specific string.

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

function add($first_number, $second_number) { return $first_number + $second_number; } $value = add(1, 3); // $value becomes 4 echo $value; // Shows $value in HTML/browser
Code language: PHP (php)

The word function tells PHP that the next thing it should see is the name of the function. Then there are parentheses 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, although they don’t have to. In PHP, a function returns the one (1) thing. In our case, our function returns a number.

It is important to know that the return value is not displayed in the browser by default.

Instead, it has to be “called” by the echo class, and the value we want to display is saved in a variable like $value.

That’s why in the above code HTML will not display any of our PHP code until we decide to show it with echo $value, which displays(echo) the value saved in the $value variable, which for this specific code will be 4.

A way to translate to human language the code above line by line would be:

Create function "add" with ("give value to first number" and "give value to second number") Start the actions that i want to happen in my function Return the function value of "first number" + "second number"; End of the actions of my function "Value" is = We call to the function created and give it values; //This is a commentary Write to be seen what we saved in "value"; // This is another commentary
Code language: PHP (php)

Basic conditionals: if, then, else

Another thing you’ll get a lot of value from when learning PHP for WordPress is coming to understand conditional processes. If you are able to live day to day is that you understand conditional logic.

And one of the fun and surprising things about learning PHP for most beginners is the pleasant surprise of how “normal” conditional logic in PHP (and many other languages) is like normal thinking.

The syntax goes something like this:

$value = 5; if ($value > 5) { execute_function(); } else { echo 'Too little'; }
Code language: PHP (php)

In the PHP code above, what will happen?

  1. The function execute_function without parameters is called and executed, or?
  2. Will the string “Too little” be printed in the HTML?

If you guessed that a function call will be made (#1), you may have forgotten that > is a “greater than” sign, and that 5 is not “greater than” 5.

I think of the above code as saying “If $value is greater than 5, call the execute_function function. Otherwise, echoToo little‘”. Or another way to see it:

"value" = 5; If (the value is bigger than 5)Execute this actions Execute this function with value(); End of actions for if Otherwise/else Execute this actions Write to be seen "Too little"; End of actions for else
Code language: PHP (php)

Other conditionals: endif and endwhile

You should also note that in the code example above, our logic actions were constrained by “curly brackets”, {}.

This is common in PHP, and many other languages. These brackets are a unique and useful delimiter of the difference between different conditions.

In PHP, because it is often mixed with HTML, it will sometimes logically have a <?php } ?> closing bracket. This is annoying and difficult to understand.

So, especially in situations with a lot of HTML (such as WordPress theme template files), many people like to use a more expressive condition type. This is why the following lines look similar to the above:

$value = 5; if ($value > 5): execute_function(); else: ?> Too little <?php endif; ?>
Code language: PHP (php)

There are some important changes here. The most important one is that we have changed the curly brackets to a colon “:" and an endif; .

They work the same, but one of them makes the closing of the tag very clear.

Curly brackets are problematic, especially when you have moved from HTML to PHP and vice versa. This is common in WordPress theme files, due to what are called WordPress conditional tags.

But before we get to that: The other change we made is that we changed echo 'Too little'; into simply the literal words “Too little”, outside of PHP tags. Doing echo from PHP code is the same as simply putting a value outside PHP tags in a file.

The same PHP logic applies in those scenarios. So in this case, “Too little” would only be displayed when $value was less than or equal to 5.

PHP and HTML for theme development

The main reasons to understand PHP tags like endif is that you’ll see them a lot in the PHP files of WordPress themes. 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 -->
Code language: HTML, XML (xml)

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

The most important thing to learn from it: The fact that PHP tags end and start inside the document (the template-parts/content/content.php file of the Twenty Nineteen theme), and that a “WordPress conditional tag” function called is_singular is called to change the markup of a function that displays the WordPress post title.

The function that displays the WordPress title is called “the_title”, and its arguments here are the specific markup of the_title() (for before and after whatever you entered in its “Title” field).

This code contains HTML in PHP strings (something common in WordPress), and uses an “input template tag” and a “WordPress conditional tag”. This is something you see a lot in WordPress.

To summarize

If this was your first PHP tutorial, congratulations on making it this far! I’m sure you’re feeling a little lost.

I covered a lot quickly, and if your brain is like mine, it all seemed a bit… abstract. That’s the first part that is hard for beginners to learn PHP.

I think the most important thing you should do if you’re trying to learn PHP for WordPress development is to try some WordPress code on your own. The things you learn when they solve your problem are much easier to remember than if I (or other writers) tell you that information 100 times.

Using PHP in WordPress is something that takes a lot of practice and work. But it also makes you more valuable and attractive to future clients, to yourself in maintaining your own WordPress sites, and it makes it possible for you to collaborate with WordPress itself.

And it’s not like you’re going to get it right away. This has been “basic and simple PHP,” after all. But I think it’s a good time to get going and learn PHP for WordPress.

Do you want to continue learning PHP?

If you want to continue this adventure of mastering WordPress development by learning PHP, here are some resources that can help you along the way:

Click to rate this post!
[Total: 0 Average: 0]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top Skip to content