What is the functional difference between include and require, and when would you use require_once over require?
Last updated
Both include and require pull another file into the current script; the difference is failure handling: a missing require is fatal, a missing include only warns.
include: On failure emits anE_WARNINGand the script keeps running. Use for optional pieces (a sidebar, a template fragment).require: On failure emits anE_ERRORand halts execution. Use for essential dependencies (config, core class, bootstrap).require_onceoverrequire:It guarantees the file is included only once per request, preventing fatal errors from redeclaring functions or classes.
Prefer it for files defining functions/classes; plain
requireis fine for templates you intentionally include repeatedly.