Include files in PHP are used in appending various global or config files. We can include .php files via include and require functions.
Index
There are two methods to include a file in PHP.
We can write global functions and classes in a config file and include them where it is needed.
Read Also: PHP OOPs
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 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.
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!
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Hey Angular geek, if you are also having Error TS2305: Module ‘”{FilePath}/app.component”’ Has No Exported…