Beginner, or programming expert, it doesn't matter your coding skill level, PHP is a great language to use for nearly all types of web dev projects. When working with web projects, you'll have tasks that require to be run on the client's machine and others that don't. But why does PHP require a server to run, when things like Javascript don't? And what is the term "back-end code" referring to? We'll explore these questions including how you could get set up on your own device and start playing around with the hugely popular PHP programming language.
What is PHP?
PHP is a widely used programming language, and its name is actually a recursive acronym for PHP itself: 'PHP: Hypertext Preprocessor.' This term refers to the fact that the 'P' in the acronym stands for the acronym itself (PHP), creating a self-referential loop. The primary purpose of PHP is to enable the creation of dynamic web content. It achieves this by seamlessly integrating with HTML and providing a human-readable programming language that facilitates tasks such as database access, form handling, and session management. Now we know what PHP is, let's explore why it requires a server to run its code.
Why does PHP need a server?
PHP in its very nature is a server-side programming language. This means in order for the code to work (your written code), it requires a server with PHP installed on it. By this, we mean a standard web browser like Firefox or Chrome can not natively run PHP code. Generally, we separate programming languages into two main areas. First, client-side, that's everything that runs in your web browser. This works the same way on your mobile, tablet, and desktop. The most popular example of this is JavaScript. The most popular client-side (or front-end to give it another name) programming language out there. Native JavaScript runs in the browser and therefore does not require a backend server for it to be run. You can even run your own JavaScript right now in the web developer tools. Secondly, there's the server side, that's where PHP sits. It runs natively on the operating system, as opposed to running in a web browser, so therefore PHP is what's called a server-side programming language.
What is meant by back-end code?
The term back-end code or simply back-end, (when referring to websites) is the word used to explain that the code run is processing behind the scenes, in this case on a remote server, which would be the website you're viewing. When you request a webpage, the server will process that request via PHP code in the particular file, using the PHP engine. It executes the commands, then generates a response back to the user, via HTML. This means that it's possible to have dynamically generated content, as PHP runs before sending it back to the client (the browser).
How to run PHP on your local machine
Another great thing about PHP is that getting started it's very easy thanks to the pre-build PHP local hosting applications, such as WAMP for Windows and XAMPP for Apple Macs. To get set up quickly with PHP, instead of installing it manually on your machine, using one of the pre-build applications will help save you time, especially if you want to get started with PHP. WAMP and XAMPP names refer to the server stack. Windows it's Windows, Apache, MySQL, and PHP. With Mac, it stands Cross-platform, Apache, MySQL, PHP, and Perl. With XAMPP it is also possible to install on Windows as it's multi-platform. The great thing about the pre-installs is it comes with other software, namely, MySQL, which you can use to create database-driven websites.
Installing WAMP on Windows 10 and 11
To install WAMP, download the Windows installer, install it, open the application, and then visit localhost in your web browser.
Installing XAMPP on Apple Mac
To install XAMPP, download the Mac installer, install it, open the application, and then visit localhost in your web browser. With XAMPP it normally maps to port 8000.
Once you've got PHP installed, you can script a PHP script, add PHP code to it, and view it in your browser. All PHP files must have the .php file extension. If you want to install PHP on a remote server and have Plesk or cPanel, PHP can be installed a managed via these control panels. If not, PHP can be installed directly onto the operating system, or used with a platform as a service such as Docker.
Once installed, to test that PHP is working as expected, simply put an index.php file into your route location with the following code. Then navigate to that location in your browser and you should see on screen the words "Hello World".
<?php
echo "Hello World";
?>
Conclusion
Next time you visit a website that is running PHP you'll know a little more about how it works.
- PHP code runs server-side, on the origin not in your browser
- You cannot run PHP code inside a browser like you can with JavaScript
- You don't need a remote server to run or experiment with, you can install WAMP or XAMPP locally
- PHP couples very well with databases like MySQL to allow you to create a database-driven website
- You can extend the basic PHP install with extra modules and libraries to enhance PHP even further