PHP Include and Require
An include or require executes PHP from another file. Keep that path under developer control and distinguish a required dependency from an optional fragment.
How it works
require fails when its required file cannot be loaded, while include has a different warning/failure behaviour. The _once forms avoid executing the same resolved file repeatedly in one request. Use __DIR__ for a fixed relative dependency instead of depending on the current working directory. Larger projects normally use an autoloader for classes. Never turn a query parameter into a code path.
Run it locally
Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.
php -l include-require.php
php include-require.phpDownload this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.
Example
<?php
declare(strict_types=1);
require_once __DIR__ . "/lending-policy.php";
require_once __DIR__ . "/lending-policy.php";
echo maximumLoanDays(), " days\n";
Expected result
21 daysThe helper is a second complete source file shown with the lesson. Loading it twice through require_once does not redeclare its function. The call then uses the explicitly named policy. The review writes both lab files into its private source directory; it does not include files from an external server or a visitor-supplied path.
Supporting file: lending-policy.php
<?php
declare(strict_types=1);
function maximumLoanDays(): int { return 21; }
Check the official PHP manual for detailed version and API contracts.