Have you ever asked yourself "What is PHP?". You've probably read or heard the term at least once. My experience shows that there is usually still little idea of what PHP is and how it works. This article is intended to fill this knowledge gap and will help you if you are new to PHP and have little technical knowledge.
PHP is first and foremost a scripting language. It was developed in 1995 and was originally called "Personal Home Page Tools". Nowadays it is referred to as a "Hypertext Preprocessor", which is a more appropriate name, as PHP generates server-side input for HTML - so PHP makes HTML dynamic.
Along with HTML, CSS and JavaScript, PHP is one of the standard languages on the Internet. Most people only get around PHP when building their website because there are content management systems (CMS) such as WordPress, which offer the PHP part as a ready-to-use solution. With PHP, however, you learn the ability to modify your CMS (and therefore WordPress in particular) in any way you like. This can be done in the form of the functions.php file or in the form of a plugin. If you have already familiarized yourself with WordPress, you will have noticed that the use of plugins can enhance your website in many ways.
PHP versions
The latest version of PHP is 8.1. On the official PHP website you can see which PHP versions are still actively supported and which are not. Some hosters do not yet offer PHP 8 for customer websites. This is because this PHP version is not yet fully compatible with WordPress or with all WordPress plugins.
With WordPress hosting with Raidboxes, you can change the PHP version of your website with just one click - PHP 8 and PHP 8.1 are also available. We explain how to properly test a new PHP version in our help center. If you have any further questions, please contact us via the live chat.
General concepts in PHP
As a link to the database, user input such as form data is traditionally processed with PHP and the content is used on the server side or entered directly into the database. If you have a contact form on your website, for example, the processing of a request will certainly be done via PHP.Â
PHP files can be integrated into other PHP files using the include command. This is useful to avoid redundant code and to separate certain functionalities. This also makes debugging easier, of course. This is also useful if the same element is to be used on several pages, for example in the header.
PHP - a server-side language
One peculiarity of PHP is that scripts are always executed on the server. If you have written a PHP script, you upload the file to your server and can then call it up there via your web browser by specifying the path. So according to the pattern "address of the server"/subfolder/filename.php. If you look in the address bar of your browser, many websites have the ending .html, but this also works with .php files.
The server must of course also be PHP-capable, i.e. the PHP interpreter must be installed. However, this is the standard for today's web servers.
This also means: If you want to develop PHP, you must either have access to the file structure on your web server (via FTP) or you can set up a local server with PHP for development. There is the well-known XAMPP package, which was designed precisely for this purpose and contains everything you need for local development.
If you want to display a random number on your website, for example, you create it in a PHP script. HTML is not able to perform such an operation on its own. In the following example, a random number between 1 and 5 is displayed on your website:Â
Dieses Beispiel demonstriert, dass hierbei Abschnitte geschrieben werden, die entweder aus PHP Code oder aus HTML Code bestehen und vom Server entsprechend interpretiert werden. PHP Code wird immer von den beiden Tags “<?php” und “?>” umschlossen. Alles, was sich außerhalb dieser Tags befindet, ist HTML Code und wird vom Server nur gesendet, aber nicht verarbeitet. Du verwendest die PHP Tags aber beim Schreiben des Codes genau so, wie du HTML Tags verwendest.
File extension
As soon as you want to execute even a single line of PHP code, the file extension must be .php instead of .html. Only PHP code that is also in .php files is executed! You should therefore pay attention to the file extension when using PHP.
The server executes the script when the file is loaded, which in most cases results in values for variables that are displayed in some way with HTML on the final website.
I now do the same with JavaScript:
JavaScript also generates dynamics on a website. The big difference is that the random number in PHP can only be generated by the server and that the browser only receives the final result from the server. The browser is not aware of the process behind it. If you look at the source code of the page, you will see a number in the HTML code that could have been entered manually. JavaScript, on the other hand, is executed on the client side (i.e. in the browser). Therefore, you can also view the JavaScript source code of a page in the browser.
PHP seems a bit chaotic to many people because it is so interlaced with HTML. There are so-called template engines that "separate" the PHP code from the HTML, such as Smarty, if you want to delve deeper into PHP. However, if you only want to use PHP in the WordPress context, this is not necessary.
Data types in PHP
PHP is a weakly typed language. In simple terms, this means that the PHP interpreter finds out for itself what data type a variable is. In other, strongly typed languages such as Java, the data types for each variable normally have to be specified directly by the person at the computer so that the program can be executed at all. In PHP, a simple assignment is sufficient, as the following example demonstrates:
At this point, it is only important to understand that PHP normally determines the data type automatically. However, the data type of a variable can also be converted manually in the code using typecast. This follows certain rules and restrictions. Unfortunately, this is beyond the scope of this article - if you want to read more about this, I recommend this article on the official PHP website.
In short, the PHP interpreter recognizes all content as corresponding data types. This is very convenient and user-friendly, but of course also a potential source of errors. My experience with PHP so far, however, is that errors in this regard are rather rare.
Objects and classes in PHP
A class can be used to describe certain objects. This is done via attributes, which are the properties of an object. Methods can be used to represent certain behavior of the object - in other words, the question here is "What can my object do?"
You always proceed in the same way when creating objects: Describe the object generally as a class using attributes and methods and then create an instance of this class at the required point in your code. Object orientation is often difficult to understand at first, so I'll explain it briefly using the example of a ball:
This part is saved as a fixed definition in the ball.php file. At this point you have only defined what a ball is (by default) and what it can do, but there is no ball object in your code. The ball is always created at the point (or in the file) where it is needed.
You have now created a red ball with a diameter of 20 cm. If you want to create the blue standard ball with a diameter of 30 cm, simply instantiate the object withÂ
To check the diameter of the ball, you could add this:Â
If you now call up this PHP file on your server using a browser, the diameter of the ball is displayed as a number.
What you do with this ball now is up to you. Do you want the ball to be displayed graphically and for two people to kick it back and forth? Do you want the ball to become a dynamic style element on your website? Do you want to assign certain behaviors to the ball via so-called methods, for example that it can roll or be kicked? There are many possibilities! As this article is just an introduction, I recommend that you continue reading here. Object orientation is a big topic in its own right.
Arrays in PHP
If you are not yet familiar with arrays: They are like tables in which a specific content is assigned to a specific key. This content can be accessed via the key. It's a bit like writing a list of numbers and asking yourself "What is the third number in the list of numbers?" The answer would be the number in the third position. Or in other words: the content of the line with key 3.
Arrays also work a little differently in PHP than in other languages - they are implemented in the form of a so-called map. To compare it with Java again: in Java, an array needs a key in the form of a number, for example myLittleArray[5]. Here, the "lines" are simply counted up starting with 0. In PHP, on the other hand, strings are used as keys to assign terms to the content. For more details and examples, you can take a look here.
"*" indicates required fields
PHP in WordPress
WordPress also works using PHP. Most plugins are written in PHP themselves and access the WordPress backend and database via PHP. You can read more about this in the official WordPress documentation. WordPress was designed in such a way that absolutely no PHP knowledge is required for the basic functionality. However, PHP is necessary for almost every individual customization.
PHP Troubleshooting
Errors occur in every programming language, which is why some form of debugging must be carried out in every language. PHP offers a range of convenient functions for this.
error_log
This line enables troubleshooting through server logs - but for this you need access to the server's file structure (usually via FTP). If you do not have access to the server, you will have to display error messages on the website, which is usually undesirable. In the case of PHP errors, the browser may only display a blank page - PHP errors are therefore all the more serious for your website. If you use WordPress, WP Debug can help you here.
Again, there is a difference between JavaScript and PHP: If your JavaScript code contains an error, only a single element may not work. If the PHP script contains errors, you will normally see nothing but a white screen (unless you have activated the error messages on the website).
Incidentally, errors can occur in PHP files if the ?> tag is left at the very end. The ?> tag should be omitted if the file only consists of PHP code.
Conclusion
PHP is a somewhat more complicated language. However, it's worth learning - at least if you want to do more than just use ready-made WordPress themes and plugins. Or even if you've reached your limits with JavaScript, HTML and CSS. If you want to change your WordPress theme or write your own plugins, there's no getting around PHP. You also need PHP for all database-related matters - and even SQL, at least if you want to stick with the standard. A tutorial is available if you want to learn PHP.
Your questions about PHP
What questions do you have for Patrick? Feel free to use the comment function. Do you want to be informed about new posts on WordPress and web design? Then follow us on Twitter, Facebook, LinkedIn or via our newsletter.