What is the difference between include and require in PHP?
Why Interviewers Ask This
This is a classic screening question for PHP roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
Both include and require insert the contents of another PHP file into the current script at the point they are called. The key difference is error handling when the file is not found: include produces a warning (E_WARNING) and continues executing the script. require produces a fatal error (E_COMPILE_ERROR) and halts execution. Use require for files that are essential to the application (configuration, database connections), and include for optional parts. Their _once variants (include_once, require_once) ensure the file is included only once, preventing function redeclaration errors.
Pro Tip
This topic has PHP-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.