November 19, 2026 - 11:04
PHP Lesson 04 — Organize the Code with Functions and Files Image
PHP Tutorials

PHP Lesson 04 — Organize the Code with Functions and Files

0 Comments

Find the button text

Search for Preview task in lesson three’s public/index.php. On the way you pass the form rules and the task array. Let’s move the code in two small steps and check that the page still works after each one.

Move the functions first

Create app beside public. Cut e() and validateTitle() out of index.php and put them after the opening <?php in app/functions.php. Keep declare(strict_types=1); at the top of index.php. You can add the same declaration at the top of the new PHP file.

Where the functions used to be, add this line.

PHP
require __DIR__ . '/../app/functions.php';

__DIR__ in public/index.php gives the path to public. Go up one level with /../, then into app. Reload and submit three spaces. If the same error appears, the validation function is loading from its new home.

Then move the HTML

Cut everything from <!doctype html> to the end of index.php and save it as app/view.php. The file begins with HTML and already opens PHP where needed.

Remove the final ?> from index.php, and put this after the task array.

PHP
require __DIR__ . '/../app/view.php';

The variables are ready when PHP runs the view. Move this line above the initialization and the view would be looking for values that have not been set yet.

Change the button label in app/view.php. Then set the limit in app/functions.php temporarily to 8. “Buy tea” should pass and “Return the book” should fail. Restore 120 before moving on. You now know where to change the layout and where to change the rule.

Complete working files

Start with the files that change in this lesson. If you completed the previous lesson, the other files are already in your project.

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

require __DIR__ . '/../app/functions.php';

$error = null;
$input = '';
$preview = null;

if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
    $input = is_string($_POST['title'] ?? null) ? $_POST['title'] : '';
    $preview = validateTitle($input);
    if ($preview === null) {
        http_response_code(422);
        $error = 'Enter a title of 1 to 120 bytes.';
    }
}

$tasks = [
    ['title' => 'Read the lesson', 'done' => true],
    ['title' => 'Run the example', 'done' => false],
];

require __DIR__ . '/../app/view.php';
app/view.php
PHP
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Task list</title>
<h1>Task list</h1>
<?php if ($error !== null): ?>
  <p role="alert"><?= e($error) ?></p>
<?php endif; ?>
<form method="post" action="/">
  <label for="title">Task title</label>
  <input id="title" name="title" value="<?= e($input) ?>" required>
  <button><?= 'Preview task' ?></button>
</form>
<?php if ($preview !== null): ?><p>Preview: <?= e($preview) ?></p><?php endif; ?>
<?php if ($tasks === []): ?><p>No matching tasks.</p><?php endif; ?>
<ul>
<?php foreach ($tasks as $task): ?>
  <li>
    <?= e($task['title']) ?> — <?= $task['done'] ? 'Completed' : 'Pending' ?>
  </li>
<?php endforeach; ?>
</ul>
</html>
app/functions.php
PHP
<?php

declare(strict_types=1);

function e(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

function validateTitle(mixed $value): ?string
{
    if (!is_string($value)) {
        return null;
    }
    $title = trim($value);
    return $title !== '' && strlen($title) <= 120 ? $title : null;
}

After saving the files, run php -S 127.0.0.1:8000 -t public in the project folder and open http://127.0.0.1:8000/. If the server is already running from that project folder, refresh the page.

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment