If you're new to PHP, setting up your first project may be a pain. I'll walk you through to make this process as painless as possible. If you're familiar with running basic html pages in VS Code, this will be similar, but without the convenience of "Live Server". What we are setting up, is a small registration form, but starting in this post with outputting "Hello World" onto a web page, along with a way of running it on a web server. The final product can be found on my GitHub.
So, we need to run our page in our browser. The easiest way I've come across is by using XAMPP. Download this for whichever OS you're running. Keep track of where this file is downloading, it's important. You can un-check a few of the download options, you only really need PHP, Apache, and MySQL. Once you're done, open up the XAMPP Control Panel. Pressing start on the Apache server, click on Admin. This will navigate you to localhost/dashboard. This is simply a file path which is being served by the Apache web server, so we just need to add some PHP files to this path as options.
This is a step which seems to be skipped by most tutorials. Open up the folder where you downloaded XAMPP and you'll see a folder called "htdocs". This is where you can add projects and files to serve. I created an empty file called "php-form" and opened it in VS Code. From here, let's create an index.php file and get started.
Opening our index.php file, we can write plain HTML in here, but it's a little more than that, we can also write our PHP code here. To make sure everything is working properly, let's create our first PHP code.
<?php echo 'Hello World'; ?>
Adding this into our project, we should be able to run our PHP file. Going back to our web server, we should be able to change the file path to localhost/php-form/. If you've saved the code, this should show "Hello World" in the top left of the web page. Let's break down this bit of PHP code though.
<?php, this tells our interpreter that the next section of code is going to be PHP. On the next line, we have
echo 'Hello World';, which just means to output "Hello World". This will be directly printed onto the web page. In some programs, you may not see the closing
?> tag, in most cases, this can be omitted and it's perfectly fine. The echo keyword is something you should familiarize yourself with, it's used everywhere in PHP when displaying information on a page. This article is super helpful for more information on the echo keyword.
So, we've output 'Hello World', which is just our first step into the world of PHP. Keep an eye out, we'll be going deeper in the future. In the meantime, feel free to read through the PHP documentation.