Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 9 of 9

Mini Project: Blog System - Part 1 (Static PHP Pages)

Build the foundation of the Blog System - a PHP-rendered set of pages with a hardcoded post array, proper string formatting, code organisation with include, and correct error setup. This establishes the project structure that every subsequent module will expand.

18 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Blog Part 1 - Project Structure & Code

Blog Part 1 - Project Structure & Code
<?php
// File structure:
// blog/
// ├── config/
// │   └── app.php          (app settings)
// ├── includes/
// │   ├── header.php
// │   └── footer.php
// ├── src/
// │   └── helpers.php       (utility functions)
// ├── public/
// │   └── index.php         (entry point)
// └── .htaccess             (route all requests to public/)

// config/app.php
$config = [
    "name"    => "PHP Blog",
    "version" => "1.0.0",
    "env"     => getenv("APP_ENV") ?: "development",
    "debug"   => (getenv("APP_ENV") ?: "development") === "development",
];
return $config;

// ----------- src/helpers.php -----------
<?php
declare(strict_types=1);

function escape(string $value): string {
    // Escape HTML output - prevents XSS from the very first function we write
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
}

function formatDate(string $dateString): string {
    $date = new DateTime($dateString);
    return $date->format("F j, Y");    // "March 20, 2026"
}

function truncate(string $text, int $length = 150): string {
    if (mb_strlen($text) <= $length) return $text;
    return mb_substr($text, 0, $length) . "...";
}

function generateSlug(string $title): string {
    $slug = strtolower(trim($title));
    $slug = preg_replace("/[^a-z0-9-]+/", "-", $slug);
    return trim($slug, "-");
}

// ----------- public/index.php -----------
<?php
declare(strict_types=1);

$config = require __DIR__ . "/../config/app.php";

if ($config["debug"]) {
    ini_set("display_errors", "1");
    error_reporting(E_ALL);
} else {
    ini_set("display_errors", "0");
    error_reporting(0);
}

require_once __DIR__ . "/../src/helpers.php";

// Hardcoded post data (will be replaced by DB in Module 6)
$posts = [
    [
        "id"         => 1,
        "title"      => "Getting Started with PHP 8",
        "excerpt"    => "PHP 8 brings match expressions, named arguments, union types and much more.",
        "author"     => "Alice Johnson",
        "created_at" => "2026-01-15",
        "views"      => 1245,
        "slug"       => "getting-started-with-php-8",
    ],
    [
        "id"         => 2,
        "title"      => "PDO vs MySQLi: Why PDO Wins",
        "excerpt"    => "Choosing the right database extension affects your code quality and security.",
        "author"     => "Bob Smith",
        "created_at" => "2026-02-03",
        "views"      => 876,
        "slug"       => "pdo-vs-mysqli-why-pdo-wins",
    ],
    [
        "id"         => 3,
        "title"      => "Securing Your PHP Applications",
        "excerpt"    => "Covering SQL injection, XSS, CSRF and password hashing best practices.",
        "author"     => "Carol White",
        "created_at" => "2026-03-01",
        "views"      => 3120,
        "slug"       => "securing-your-php-applications",
    ],
];

$pageTitle = escape($config["name"]) . " - Latest Posts";
include __DIR__ . "/../includes/header.php";
?>

<main class="container">
    <h1>Latest Posts</h1>
    <?php foreach ($posts as $post): ?>
    <article class="post-card">
        <h2><a href="/post/<?= escape($post["slug"]) ?>">
            <?= escape($post["title"]) ?>
        </a></h2>
        <p class="meta">
            By <?= escape($post["author"]) ?> &bull;
            <?= formatDate($post["created_at"]) ?> &bull;
            <?= number_format($post["views"]) ?> views
        </p>
        <p><?= escape(truncate($post["excerpt"])) ?></p>
    </article>
    <?php endforeach; ?>
</main>

<?php include __DIR__ . "/../includes/footer.php"; ?>

Quick Quiz

Module 1 Quiz

Module 1 Quiz
Question 1 of 5

Which operator should you ALWAYS use instead of == when comparing values in PHP?

Tip

Tip

Practice Mini Project Blog System Part 1 Static PHP Pages in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Mini Project: Blog System - Part 1 (Static PHP Pages)

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Blog System Part 1 Static PHP Pages from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Mini Project Blog System Part 1 Static PHP Pages is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done