PHP File Inclusion: The Two Choices That Matter
What is the difference between `include`, `require`, `include_once`, and `require_once` in PHP, and when would you use each one?
what they're testing: The interviewer is testing whether you can choose the right file-loading behavior based on failure handling and duplicate evaluation.
All four include and evaluate another file at the point PHP reaches the expression. The names encode two separate choices. include emits an E_WARNING when it cannot load the file, so the following code can still run. require has the same inclusion behavior, but failure produces an Error exception in PHP 8; before PHP 8 it was an E_COMPILE_ERROR. If that failure is not handled, normal execution stops.
The _once suffix adds a duplicate guard. include_once behaves like include, and require_once behaves like require, but PHP skips the file if it has already been included during that script execution. This avoids evaluating declarations or side effects twice. It does not make the file optional and does not cache it across requests.
Choose based on whether the rest of the request can work without the file. A required bootstrap or configuration file usually calls for require or require_once. A nonessential template fragment may suit include. Use a _once form when multiple code paths may reach the same file; use the plain form when repeated evaluation is intentional.
Where people slip
the tempting wrong answer, and what's actually true
All four forms stop the script when the target file is missing.
`include` and `include_once` emit an `E_WARNING` and allow execution to continue, while the `require` variants fail with an error.
The `_once` variants include a file once for the lifetime of the application.
They guard against another inclusion only during the current script execution, not across later requests.
`require` loads files before execution, while `include` loads them at runtime.
Both include and evaluate the file when execution reaches the expression; their documented difference is the failure behavior.
If they push further
What scope does an included file run in?
It inherits the variable scope of the line where it is included. Functions and classes declared in that file still have global scope.
Can a missing `require` be caught in modern PHP?
In PHP 8, `require` throws an `Error` exception on failure, so code can catch it through `Error` or `Throwable`; before PHP 8 the failure was an `E_COMPILE_ERROR`.
When is the non-once form the right choice?
Use it when evaluating the file again is deliberate, such as rendering the same template with different variables. Otherwise, repeated declarations or side effects are a strong reason for a `_once` form.
Sources
- PHP Manual: include ↗php.net
- PHP Manual: require ↗php.net
- PHP Manual: include_once ↗php.net
- PHP Manual: require_once ↗php.net
Now answer it yourself.
Reading a strong answer is easy. Producing one under pressure is the skill the interview tests. Gapmap grades your answer against the same bar an interviewer would.
beta
The interviewer part is in the works.
The diagnostic, personal maps, and AI mock interviews are being finished right now. The notes stay free either way. Leave an email and you'll get the first-cohort invite, plus a month of Pro when it opens.