To learn PHP, the best way to start is with a simple PHP website.
Why Choose PHP for Website Development?
Various options have been available for web development over the years. It started with plain HTML, then HTML with CSS embedded or a CSS file reference. When dynamic websites came along, there were two main choices: ASP (later ASP.NET) and PHP.
According to figures (such as this W3Techs survey) PHP web development is far more popular, with almost 82 percent of websites using PHP as a server-side programming language. Compare this with just under 16 percent using ASP.NET.
It seems unlikely that ASP.NET will exist beyond its official cut-off date in 2022 in any official capacity, at least not as a web technology. PHP (a recursive acronym for PHP Hypertext Preprocessor) has proven more successful, mainly thanks to easier integration with Linux.
As Linux open-source operating systems run on most web servers, this should not come as a surprise.
What You’ll Need to Build a PHP Website
Before starting, be sure you have a plain text editor or PHP-ready development environment installed. You can start coding PHP with a tool as simple as Windows Notepad. Examples found in this tutorial have been written in Notepad++.
You should also have a PHP web server to upload your files to. This might be a remote server, or a local computer with a LAMP (Linux, Apache, MySQL, PHP) or WAMP (Windows, Apache, MySQL, PHP) environment installed. If you’re using Windows, follow this WAMP installation guide to get started.
Finally, you’ll need an FTP program to upload your files to your web server.
Understand Syntax to Code a Simple Web Page Using PHP Code
The basic syntax for PHP uses a set of angled brackets, with each function ending with a semi-colon, like this:
In terms of web pages, almost every use of PHP relies on the echo statement. This instructs the browser to output the text and content in the quotes. For example:
<?php print("Hello World")?>
Note that the HTML is also included within the quotes. The output for this would typically appear as:
How to Create a Website Using PHP: Get the Structure Right
Whatever code you're writing your website with, you will need to know the structure of the site before proceeding. This tutorial will show you how to create a single page from reusable PHP files. These might be used to make additional pages, or you may choose a different approach.
Whatever shape you foresee the site developing, take the time to jot a quick plan on a piece of paper. You can then refer to this, perhaps to check the intended content, or see what page to link it to.
Our basic PHP website is going to feature a home page, including biographical information and some images.
For this simple PHP website, you're going to create a single PHP page populated by content from three HTML pages. The index.php file you create can then be edited by adjusting the words and images from the original HTML files.
The code examples shown below are screenshots. You'll find the original code in my GitHub repository, which is free for anyone to download.
Read on to learn how to create a PHP website step-by-step.
To create a website using PHP, you'll need to construct three web pages. These are based upon the basic structure of header, body, and footer.
As you might guess, the header includes title information. However, information for the browser is also included, such as the HTML standard in use, along with CSS references.
Start by creating a file called header.html then add the necessary header information.
For this example, we've provided a basic CSS file, which you will see is referenced in its own /css/ directory. This file will be called when the page loads in your browser and apply the required font and layout.
Put Content in Your PHP Web Page Body
Every web page consists of a content section known as the "body". This is the part of a page that you read---what you're reading now is the body of this page.
Create a file called body.html and add the information you want to include on the page. I've included biographical details from my MakeUseOf author page, but you can add whatever you like.
The footer section of the web page is next. Create this as footer.html and add some content. This could be copyright information, or perhaps some useful links for anyone visiting your page.
It might be something like this:
With the code added, save the file.
Putting Your Simple PHP Website Together
With three separate HTML files in /html/ you can use PHP echo to compile them into a single page.
Create a new PHP file called index.php with the following three lines in it:
<?php echo file_get_contents("html/header.html"); ?>
<?php echo file_get_contents("html/body.html"); ?>
<?php echo file_get_contents("html/footer.html"); ?>
Save, upload to your server, then browse to index.php. You should see the completed web page in your browser. Keep in mind that the actual PHP file you have open in your browser consists of just three lines.
Finally, you can add a little PHP flourish with the final line. Include a copyright notice, with an always-updated year:
<p>Copyright © CM Cawley <?php echo date("Y"); ?></p>
This will appear in the index.php file following the footer. Notice how the echo date("Y") statement displays the current year in four digits. You can change how this is displayed by referring to this W3Schools list of options. For example, a lower-case "y" would display the year in two-digit format, rather than four.
Use CSS to position and style it, as you would with any other element. Find the CSS for this project in the GitHub repository, along with the other simple PHP website code.
Well done---you've just created your first PHP website from scratch. Now it's time to look at more PHP website examples to improve your skills
Is PHP the Best Choice for Coding Websites?
However, if you're looking to get started with PHP web development, it's smart to have an appreciation of the basics. If you understand the website building blocks of HTML, CSS, and PHP, you're well on the way to success.
About The Author