2) PHP Include vs Require

Include and require files are important as it helps us to stay organized and it is one of the very useful features in PHP as we do not have to repeat code many times. If we want to use a certain piece of code for the whole website we do not have to copy paste the code we can just include the file. And we do not even have to modify all files code to simply edit a piece of code that is common to all files.

We can use include and require to

  • Library of functions.
  • Layout sections: header, ¬†footer, navigation, sidebar
  • Various functions to include files

    • include()
    • require()
    • include_once()
    • require_once()
    • Include and require works exactly same except it raises an error if the file is not found or loaded. Use require only if the file is essential for operating the rest of script.

      Include not stop the execution if a code if the included file not found, however, require will give a fatal error and stops the execution of a programme

      Include_once and require_once works the same way as include and require except they also keep track when a file is loaded and if the file is included again it will skip over it.