Simplify Your Coding By Using Some Simple PHP

Posted By: Proper Noun Staff Posted On: December 9, 2019 Share:

PHP is a server side language that is used for web development and can be easily inserted into and used with HTML. PHP has a wide variety of uses and applications, but today I’m going to talk about the include statement. By using a PHP include statement you can save yourself a ton of time and keep your code much cleaner. For this example, I’ll use the statement for my navigation.

First things first, you’ll need to fully code out the html and css of at least one page on your website. If you’ve saved your file as “index.html” create a new copy by clicking “Save As” and saving as “index.php” so that your PHP will be able to execute. Once you’ve done this, locate the section of your code with your navigation in it. Here’s what my code looks like:

<div>
    <ul>
        <li>Link 1</li>
        <li>Link 2</li>
        <li>Link 3</li>
        <li>Link 4</li>
        <li>Link 5</li>
        <li>Link 6</li>
    </ul>
</div>

There is corresponding CSS, as there should be with yours as well, but it is not important for the purposes of this post so I’m not including it to keep this easier.
Next you will create a new file called “navigation.php” and save it in your directory. We are going to use this file to control the navigation throughout the entire site.

Once you’ve saved the new navigation.php file, go back to your index.php file and copy all of the code for your navigation that is inside the class “navigation” and then paste it inside of navigation.php and save. It should look like this:

<ul>
        <li>Link 1</li>
        <li>Link 2</li>
        <li>Link 3</li>
        <li>Link 4</li>
        <li>Link 5</li>
        <li>Link 6</li>
</ul>

Now, back in your index.php file, replace the text you just copied inside the class “navigation” with the following and save:

<?php include 'navigation.php'; ?>

If done correctly, when you preview your page either online or on your test server, the PHP include statement should pull the unordered list inside of “navigation.php” into the class “navigation” and apply it’s styling.

Now, if and when you need to make changes to your navigation, links, pages, etc., you’ll only have to change one file, one time, not multiple times on multiple pages. This same method can be applied to several aspects of your site. For instance, your entire header area, including your logo, navigation, and so on can be in a file called “header.php” which can be called in all of your pages. This way, if you change your logo down the line, you can instantly change it site wide just by changing one file.

This is just a sample of what PHP can do in improving your sites functionality and usability. For more information on PHP and the many functions it can perform just check out the PHP Website.

Proper Noun Staff

Proper Noun Staff

In-House Writing Team

Read informative articles, insights, and other resources right from the experts on the Proper Noun team.

Copyright © Proper Noun 2024