How to use include and require in PHP

Learn how to use include and require statements in PHP with our informative guide. We’ll show you how to import external files into your PHP code, and explain the differences between the two statements. Start optimizing your PHP applications today!

Developing a web application can be a really difficult thing to do. You need to plan each sub system separately, analyze their connections in terms of overall performance, map out models to use so you can implement persistence to store the data safely and you should absolutely endeavor to create a staggering and a robust user interface. The user is the heart of your application after all.

In order to do all those things and actually produce something great for the user you really need to create connections between files so you can move the data around easily. Think about it. If you cannot move the data you cannot populate pages with information to show to the user, you cannot interact with a database to refresh the content and you cannot create dynamic data modifications. Include and require functions come to fill in the blanks and solve this problem. If you don’t know what functions are and do, I suggest you visit this article here. First things first, what does the include function do.

The include() function will import some other PHP script’s definitions and you will be able to use those variables to that very page’s code logic that include was written. In Layman’s terms, you expose all the variables from a specific script to the one you called the function from. Note also that you should use include if the file is not required for the application to work. In case the included file is not found, it will not create an error. It will however throw a warning.

If you think about it, that’s a great advantage for your web application’s code base. To be more specific, the advantage is the portability of your code and the ability to access certain definitions from virtually anywhere. Let’s put that into perspective. Say that you created a connection to a database and you want to expose that config variable that’s going to help you execute queries. Well, you only have to include the specific config.php in this case and then you’re off the door to execute queries without issues.

// config.php
$_host = 'artisan_server';
$_pass = 'artisan%pass;
$_user = 'artisan_user';
$_db = 'artisan_db';
$mysqli = mysqli_connect($_host, $_user, $_pass, $_db);

Now that you have created your config file, you need to expose that $mysqli object to the rest of the app in order to provide the tool that makes changes to the database.

// home.php
include 'config.php';
echo '<pre>';

As you can see, we included the file config.php within the home.php file and now we can use the connection object to either print it on the screen (for whatever reason) or execute queries. But what’s the difference with require?

require() essentially includes the designated script’s contents but you need to make sure that the imported script will be always available for usage since it is required from the application. In case the file is missing, require will throw an error and terminate the app. It will also generate a warning, indicating the missing file’s path.

// home.php
require 'config.php';

// prints:

Warning: require(config.php): Failed to open stream: No such file or directory in (PATH) on line X

Fatal error: Uncaught Error: Failed opening required 'config.php' (include_path='PATH') in PATH:X Stack trace: #0 {main} thrown in PATH on line X