PHP Basics
Did you know that PHP is one of the most popular programming languages? In fact, according to https://w3techs.com, PHP is used by 83% of the websites in the entire world. Facebook, Aliexpress and Wikipedia are just a few of the famous sites that utilize PHP, for example.
So, if your website is based on WordPress and you want to tweak its aspect or boost its functionality, you will need to learn a bit of PHP, the acronym for "PHP Hypertext Preprocessor". Yeah, don't ask me why the first "P" stands for "PHP"; apparently, the language creators thought that this was cool.
WordPress utilizes the PHP programming language, so it's the only way to go if you want to create a child theme, and thus be able to update WordPress anytime you need to, without destroying your website.
Of course, to modify the aspect of a website you will also need to know a bit of CSS and HTML. However, since PHP is a bit more advanced in comparison with those two programming languages, it will be much easier to understand them after you've learned PHP.
One word of advice, though; it is true that PHP is much more powerful than CSS or HTML, but its power can quickly lead to disasters. Here's an example: if you misplace a CSS bracket, your website may not load the desired font, replacing it with the default font. However, if you misplace a PHP bracket, your website may stop working for good. Hopefully, this hasn't gotten you discouraged, because if you want to extend or modify WordPress' functionality, PHP is the only way to go.
Let's get started! PHP files are plain text files that have a .php extension. By reading this extension name, web servers know how to process their code. Each PHP file has opening and closing tags that look like the ones below.
// your code goes here
?>
The good news is that you can mix PHP and HTML without problems, including code that belongs to both programming languages within the same .php file. Basically, you will want to use PHP code to process data (read or write information from/to a database, etc.) and HTML code to display the results.
If you took a good look at the tiny snippet above, you have noticed that it also includes a comment line. Simply add two slashes at the beginning of a PHP line to turn all the characters that follow them into a comment.
It is very important to use comments in your code, because it will be much easier to remember what you did when you will look at the code later on. Not only that, but comments will be extremely useful if you plan to share your code with others.
/*
Comment blocks are useful if you've got long comments
and you don't want to use // at the beginning of each line
*/
?>
The code above shows a different method of using comments in your PHP files. It's the perfect way of integrating huge comments in your code.
PHP utilizes functions to process data and return results. Here's the syntax for a typical function.
function my_php_function() {
// put your PHP code here
}
To run a function, you will need to call it using its name, like this.
my_php_function();
It is essential to add the semicolon at the end, but only when you are calling the function (executing it). Also, each line of code (excepting comments, of course) must end with a semicolon.
Take a look at the picture on the right to see the full source code for a typical "Hello World!" application, which was written in PHP.
This was just an introduction to PHP, of course. To get started, visit https://codex.wordpress.org, where you will find lots of tutorials and code snippets that you can play with.