Simple PHP Examples

A progressive PHP 8.5 micro-framework tutorial in 10 chapters

PHP 8.5 SQLite MIT License 10 Chapters No Bootstrap
<?php declare(strict_types=1);
// Copyright (C) 2015-2025 Mark Constable <mc@netserva.org> (MIT License)

echo new class {
    private const string DEFAULT = 'home';                  // PHP 8.3 typed constant
    private array $pages = [
        'home'    => ['Home', '<h2>Home</h2><p>Lorem ipsum home.</p>'],
        'about'   => ['About', '<h2>About</h2><p>Lorem ipsum about.</p>'],
        'contact' => ['Contact', '<h2>Contact</h2><p>Lorem ipsum contact.</p>'],
    ];
    public private(set) string $page;                       // PHP 8.4 asymmetric visibility
    public private(set) string $main;

    public function __construct() {
        $this->page = ($_REQUEST['m'] ?? '') |> trim(...) |> htmlspecialchars(...) |> (fn($p) => $p ?: self::DEFAULT);
        $this->main = $this->pages[$this->page][1] ?? '<p>Error: page not found</p>';
    }

    public function __toString(): string {
        $nav = $this->pages |> array_keys(...) |> (fn($k) => array_map(   // PHP 8.1 first-class callable
            fn($p) => "<a href=\"?m=$p\">{$this->pages[$p][0]}</a>", $k)) |> (fn($a) => implode(' | ', $a));
        return <<<HTML
        <!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <meta name="color-scheme" content="light dark">
        <title>SPE::01</title><style>a{text-decoration:none}nav{margin:1em 0}</style></head>
        <body><header><h1><a href="../">« Simple PHP Example</a></h1><nav>$nav</nav></header>
        <main>{$this->main}</main>
        <footer><small>© 2015-2025 Mark Constable (MIT License)</small></footer></body></html>
        HTML;
    }
};

Features

|>

Pipe Operator

PHP 8.5's elegant data transformation chains

🔌

Plugin System

Extensible architecture with meta.json config

🎨

Custom CSS

270 lines, dark mode, no Bootstrap

🗄️

SQLite + PDO

Type-safe queries with QueryType enum

Chapters

01

Simple

Single-file anonymous class, pipe operator basics

02

Styled

Custom CSS, dark mode, toast notifications

03

Plugins

Plugin architecture, CRUDL pattern

04

Themes

Model/View separation, multiple layouts

05

Autoload

PSR-4 autoloading via Composer

06

Session

PHP session management

07

PDO

SQLite database, QueryType enum

08

Users

User management CRUDL

09

Blog

Full CMS: Auth, Blog, Pages, Categories

10

YouTube

OAuth, API integration

Quick Start

git clone https://github.com/markc/spe
cd spe
composer install
php -S localhost:8000

Documentation

Select a document from the sidebar...