W3docs

require_once

In PHP, the "require_once" keyword is used to include and evaluate external PHP files within your code, but with the added benefit of ensuring that the file is

The PHP require_once Keyword

require_once includes and evaluates an external PHP file at the point where the statement is written — but only the first time that file is requested. If the same file is requested again later in the same request, PHP skips it. This makes require_once the safest way to pull in files that define functions, classes, traits, or constants, since loading those a second time would crash the script.

This page covers the syntax, how the "once" guarantee works, runnable examples, and how require_once differs from require, include, and include_once.

Syntax

The require_once keyword includes and evaluates an external PHP file, ensuring it is only processed once. Here is the basic syntax:

The PHP syntax of require_once

require_once 'path/to/file.php';

This includes and evaluates the contents of path/to/file.php, but only if it hasn't already been included during this request. Because it is a language construct (not a function), the parentheses are optional — require_once 'file.php'; and require_once('file.php'); behave the same.

Note: If the specified file cannot be found, require_once triggers a fatal error and halts the script. Its counterpart include_once only emits a warning and lets the script continue.

How the "only once" guarantee works

PHP keeps an internal list of files it has already loaded (resolved to their absolute paths). When it reaches a require_once, it checks that list first:

  • If the file is not on the list, PHP loads and runs it, then adds it to the list.
  • If the file is already on the list, PHP does nothing and moves on.

This is what makes require_once safe for files that define things. The following two files demonstrate it. helpers.php defines a function and prints a line so we can see when it actually runs:

<?php
// helpers.php
echo "helpers.php is being loaded\n";

function greet($name)
{
    return "Hello, $name!";
}
<?php
// main.php
require_once 'helpers.php';
require_once 'helpers.php'; // second request is silently skipped

echo greet("World") . "\n";

Running main.php prints:

helpers.php is being loaded
Hello, World!

The "helpers.php is being loaded" line appears only once, even though the file was requested twice — and the second request did not re-declare greet(), which would otherwise be a fatal error.

Examples

Let's look at some practical examples of how the require_once keyword can be used:

Examples of PHP require_once

<?php

// Example 1
require_once 'config.php';

// Example 2
function myFunction()
{
  require_once 'helpers.php';
  // Code block here
}

Both examples include and evaluate external files exactly once. This prevents the errors that occur when a file is loaded twice — most commonly redefining a function or a class.

Note: Although require_once can be placed inside a function, PHP's inclusion tracking is global for the whole request. Top-level placement is usually preferred; reach for in-function inclusion only when the load must be conditional.

require_once vs the other inclusion keywords

PHP has four inclusion constructs. They differ on two independent axes: what happens on failure, and whether the file may be loaded more than once.

KeywordIf file is missingLoads file again on repeat request?
require_onceFatal error (stops script)No
requireFatal error (stops script)Yes
include_onceWarning (script continues)No
includeWarning (script continues)Yes

The _once variants exist precisely to avoid re-running a file. With plain require, requesting helpers.php twice runs it twice — and the second run fails fatally trying to redeclare greet():

PHP Fatal error: Cannot redeclare function greet() ...

Rule of thumb: use require_once for files that define things (function libraries, classes, config that mustn't run twice); use include for files that only produce output, such as a reusable page header you intentionally render multiple times.

Benefits

Using the require_once keyword has several benefits, including:

  • Preventing errors: Ensures a file is only processed once, avoiding fatal errors from redeclaring functions, classes, or traits.
  • Efficient resource usage: Prevents redundant file loading and parsing. (Note: PHP maintains a small tracking overhead for included files, but this is typically outweighed by the cost of re-parsing large files.)

Conclusion

In conclusion, require_once is a reliable tool for including external PHP files while preventing duplicate processing and fatal errors. Use it when a file must be loaded exactly once, and choose require or include when you need different error handling or want to avoid tracking overhead. We hope this guide helps you write more robust PHP code.

Practice

Practice
What is the primary usage of the require_once statement in PHP?
What is the primary usage of the require_once statement in PHP?
Was this page helpful?