How to Build a Simple PHP Website

Want to learn how to build a basic website? Once you may have started with HTML, but nowadays the best solution is PHP. While you will need some HTML knowledge to get started, PHP has become the ultimate choice for building websites, both static and dynamic.

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 starts with plain HTML, then HTML with embedded CSS or CSS file references. When dynamic web pages came into being, there were two main choices: ASP (later ASP.NET) and PHP.

According to metrics (such as this W3Techs survey), PHP web development is far more popular, with almost 82% of websites using PHP as a server-side programming language. Compare this to just under 16 percent using ASP.NET.

It seems unlikely that ASP.NET will exist beyond the official 2022 deadline in any official capacity, at least not as a web technology. PHP (an acronym for PHP Hypertext Preprocessor) has proven to be more successful, mainly due to easier integration with Linux.

Since the open source Linux operating system runs on most web servers, this is not surprising.

What you need to build a PHP website

Before you begin, make sure you have a plain text editor or PHP-ready development environment installed. You can start writing PHP code with a simple tool like Windows Notepad. The examples found in this tutorial are written in Notepad++.

MAKE A VIDEO OF THE DAY

You should also have a PHP web server to upload your files to. This can be a remote server or a local computer with the 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 files to your web server.

Understand the syntax to code a simple web page using PHP code

The basic syntax for PHP using a set of curly braces, with each function ending in a semicolon, is as follows:

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 enclosed in quotes. The output for this will usually appear as:

How to Create Websites with PHP: Get the Right Structure

Regardless of the code you're writing your website in, you'll need to know the structure of your site before proceeding. This tutorial will show you how to create a single page from reusable PHP files. They can be used to create additional pages, or you can choose a different approach.

Whatever shape you see your site developing, take the time to jot down a quick plan on a piece of paper. You can then refer to this, perhaps to check the intended content or see which page to link it to.


Our basic PHP site will have a home page, which includes profile information and some images.

For this simple PHP website, you will create a single PHP page filled with content from three HTML pages. The index.php file you create can then be edited by adapting 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, free for anyone to download.

Read on to learn how to create a PHP website step by step.

To create a website in PHP, you need to build three websites. They are based on the basic structure of header, body and footer.

As you might have guessed, the header includes header information. However, information for the browser is also included, such as the HTML standard being used, 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'll see referenced in its own /css/ directory. This file will be called when the page loads in your browser and applies the required font and layout.

Include content in the body of your PHP web page

Every web page includes a piece of content called a "body". This is the part of the page that you have read --- what you are 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 site footer is the sequel. Create this as footer.html and add some content. This could be copyright information, or it could be some useful link for anyone who visits your site.

It could be something like this:

With the code added, save the file.

Bring your simple PHP site 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 named 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 finished web page in your browser. Remember that the actual PHP file you opened in your browser consists of only three lines.

Finally, you can add a bit of PHP to the last line. Includes a copyright notice, with one year always up to date:


<p>Copyright © CM Cawley <?php echo date("Y"); ?></p>

This will appear in the index.php file after the footer. Notice how the echo date ("Y") statement displays the current year in four digits. You can change how this message is displayed by consulting this list of W3Schools options. For example, a lowercase "y" will display the year in a two-digit format, instead of four.

Use CSS to position and style it, as you would any other element. Find the CSS for this project in the GitHub repository, along with other simple PHP website code.

Well done --- you just created your first PHP website from scratch. Now it's time to see more PHP website examples to improve your skills

Is PHP the best choice for coding websites?

Now that you know how to create a PHP website, you should know that it's not the only way to develop a website. Many frameworks already exist for dynamic, database-driven web experiences, featuring JavaScript and related technologies, and software such as Adobe Dreamweaver.


However, if you're looking to get started with PHP web development, it's smart that you appreciate the basics. If you understand the website building blocks of HTML, CSS, and PHP, you are well on your way to success.


Learn HTML and CSS with these Step-by-Step Tutorials

Continue reading


About the author

https://www.makeuseof.com/tag/build-simple-php-website/ How to Build a Simple PHP Website

Sarah Ridley

USTimesPost.com is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@ustimespost.com. The content will be deleted within 24 hours.

Related Articles

Back to top button