Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is the difference between include, require, include_once and require_once?

All four pull in another PHP file. They differ on two axes: what happens on failure, and whether repetition is allowed.

  • include — emits a warning if the file is missing and continues execution.
  • require — emits a fatal error and halts.
  • include_once / require_once — the same, but PHP tracks which files it has already loaded and skips a repeat.

Which to use: require for anything the script cannot run without — a configuration file, a class definition, a database connection. include only for genuinely optional content, such as a sidebar template. Continuing after a missing class definition produces a cascade of confusing errors instead of one clear one.

The _once variants prevent "cannot redeclare function" fatal errors when a file is reachable through more than one path. They carry a small overhead because PHP must check the loaded-file list.

Note: The right answer in modern PHP is that you rarely write any of these. Composer's PSR-4 autoloader loads classes on demand, so a single require of vendor/autoload.php replaces hundreds of manual includes.

All Php interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as