PHP Advance

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!

Nachiket Panchal

Founder & Administrator of `errorsea` Having interest in Programming & Technology.

Share
Published by
Nachiket Panchal

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

3 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

4 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

5 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

5 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

5 years ago

[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…

5 years ago