PHP Include & Required

Include files in PHP is helpful to append various global or config files. We can include .php files via include and require functions.

Include Files in PHP

There are two methods to include a file in PHP.

  1. Include() Method
  2. Require() Method

We can write global functions and classes in a config file and include them where it is needed.

Read Also: PHP OOPs

Include() Method

Include() method is used for soft include files in PHP. If the included file does not include properly, the compiler will skip the include file process and start to compile the rest of the code.

Include method will not prompt any error while runtime if a file could not be included.

Syntax

<?php

include("file_name_path");

?>

Example

<?php

include("/config_folder/demo_config.php");

?>

Require() Method

Require() method is used for hard include files in PHP. If the included file does not include properly, the compiler will throw an error and stops compiling the rest of the code.

Require method will prompt runtime error if a file could not be included.

Syntax

<?php

require("file_name_path");

?>

Example

<?php

require("/config_folder/demo_config.php");

?>

Read More: PHP Redirect pages

Conclusion

Include and Require both methods have their own pros and cons. Both methods are popular among PHP programmers.

There is no strict rule to use these methods. The usage depends on the workflow and structure of the website.

Enjoy Coding šŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
[Solved] Error TS2305_ Module Has No Exported Member in Angular CLI
[Solved] Error TS2305: Module ā€˜ā€{FilePath}/app.componentā€ā€™ Has No Exported Member ā€˜ErrorseaFilterā€™ in Angular CLI

Hey Angular geek, if you are also having Error TS2305: Module ā€˜ā€{FilePath}/app.componentā€ā€™ Has No Exported Member ā€˜ErrorseaFilterā€™ or similar error...

Close