PHP Include & Required

Include files in PHP are used in appending 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.

The Include method will only throw a warning if the file is not found but will not prompt any error during runtime.

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 stop the compilation.

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

Both Include and Required throw warnings in case, the file is inaccessible.

Conclusion

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

Require is used when the file is required by the application.

Include is used when a file is not required and the application should continue even when the file is not found.

We hope you have gained a complete understanding of the OOPs concepts in PHP programming. In case you want to learn about the other concepts, refer to our PHP Tutorial, PHP MySQL, PHP Form and PHP OOP sections. Happy Learning!

Leave a Reply

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