PHP is a powerful scripting language created for the web.
It has come a long way since it’s initial inception in 1994.
It was originally created to build websites quickly and easily and has become a lot more than that.
While many developers and companies use PHP along with extensive and oftentimes heavy frameworks, there is no reason that you have to do that; you can build websites quickly and very easily by making use of many standard PHP functions.
The purpose of this tutorial is to show you how to do just that.
PHP comes with a built-in web-server, that you can use for a development or for quick testing. Do note that you should not deploy anything to a production server with it though. For production environments, a hardened web server such as Apache2 or Nginx is highly advantageous and always advised!
First we will create a new directory as our project root:
Now that we’re in our new project, we can create our initial file, which should always be called
Now we open our
index.php file for editing and insert the following PHP code.
Note that we start a PHP file with
<?php and we strictly don’t have to end it with
?>, but you can if you want…
We can now view it in a web browser by running the following command and navigating to the URL in our browser:
We can now see our text string rendered out in the browser!
A directory structure
Organising your files is crucial to a simple project, I often go with a similar layout to the below:
In order to be able to make use of different pages and routings, we can add a simple routing system; this is why we have a
pages directory in our tree above.
Create two files under pages called
home.php, add the following:
about.php add the following:
index.php and change the contents of the file to the following:
Our project tree now looks like:
You can now view your simple website at http://localhost:8000.