PHPPHP11 min read

Include vs Require in PHP (and When to Use Each)

Learn how to split PHP code into multiple files and choose include/require safely in real projects.

Amanda Carter
December 21, 2025
0.0k0

As your PHP project grows, you do not want one giant file. You split code into smaller files and reuse them across pages.

PHP gives you: - `include` - `require` - `include_once` - `require_once`

The difference (simple and important)

### include If the file is missing, PHP shows a warning and continues.

### require If the file is missing, PHP throws a fatal error and stops.

Example

```php <?php include "header.php"; // if missing, page may still load require "config.php"; // if missing, stop (safer for config) ?> ```

When to use what

- Use `require` for critical files: - database config - authentication helpers - autoloaders

- Use `include` for optional parts: - UI parts like header/footer - optional widgets

Use *_once to prevent double loading

```php require_once "config.php"; require_once "config.php"; // will not load twice ```

This avoids duplicate function/class errors.

> Next: Sessions and cookies, how websites remember users.

#PHP#Files#Beginner