Include files in PHP is helpful to append various global or config files. We can include .php files via include and require functions.
Index
Include Files in PHP
There are two methods to include a file in PHP.
- Include() Method
- 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 š