Include (folding pages from pieces)

PHP was originally a template language, which was created for the simple folding of pieces of pages together.

Supported formats

Collation works in text form, so it is advisable to use relevant formats such as .html or .md.

When you insert a PHP file, its contents are executed as if they existed physically at the inserted location.

Folding pages and inserting common content

We often need to create several pages that share content - for example, menus.

In pure HTML, we would first create a menu page and then copy it many times. In PHP, we can automate the process.

Let's have the menu.html file, where the content is menu and index.php, where we put the content and menu.

A simple example:

<div class="page">
    <div class="content">
            include __DIR__
            . '/article/' . ($_GET['page'] ?? 'index') . '.html';
    <div class="menu">
            include 'menu.html';

This script automatically inserts the contents of the page from the / article directory and retrieves the file name according to user input (URL parameter ?page=...). If no parameter was passed, index.html is used.

So a URL might look like: and /article/contacts.html loads.

Previous chapter ((Data Sending Methods) Next chapter (Conditions and branching)

Back to Learn PHP main menu