Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
PHP include() vs require()

PHP include() vs require()

October 12, 2010 PHP Jesin A 1 Comment

php category thumbnail

If there is one feature that is highly useful and used by novices and experts alike its SSI (Server Side Includes). It reduces the need to copy paste repetitive code and you get a cleaner code. PHP has two functions for including files include() and require(). There is a lot of confusion on which to use when because both of them basically perform the same function of including the specified file but the difference comes by how they handle files.

Error handling in include()

When using the include() function if the specified file doesn’t exist a warning is displayed and execution of the following lines continue. Take a look at the code below.

<?php
include("missing_file.php");
print "<h3>Rest of the content</h3>";
?>

Here there is no file named missing_file.php, executing the code will display the following error

error in php include function
Warning displayed when an error is encountered in the include() function

As seen above a warning is displayed and the rest of the lines are executed. Now lets see how require handles errors

Error handling in require()

As the name implies the file called in the required function is definitely REQUIRED and if it doesn’t exit a fatal error is displayed and the execution stops. Let me use the similar code used above with some modifications

<?php
require("missing_file.php");
print "<h3>Rest of the content</h3>";
?>

Take a look at the error message when this code is executed

error in php require function
A fatal error is displayed when the file called in the require() function does not exist

After the fatal error the print statement is not executed.

When to use which function ?

So by now you understand the difference between PHP include() and require() functions so which is the right place to use either of these functions? If you’re using SSI (Server Side Includes) for displaying footers, banners or advertisements then it is safe to use include() function because a missing banner or ad file is not a reason to stop the execution of the whole page.

Use the PHP require() function where ever a file is REQUIRED i.e., if a user defined function or class is need by every file for performing some operations the call the file using require() function. A good example is calling PEAR files in your PHP code.

Related posts:

php category thumbnailPHP auto_prepend_file and auto_append_file php category thumbnailPHP Sessions Tutorial wordpress custom 403 error pageCustom 403 and 401 error pages in WordPress connect php with mysql thumbnailConnect PHP with MySQL php category thumbnailPHP Open Port Check Script

Tags: include, php, require, ssi

Trackbacks

  1. PHP auto_prepend_file and auto_append_file | Jesin's Blog says:
    June 30, 2013 at 2:48 pm

    […] auto_prepend_file and auto_append_file. These two PHP directives perform the same function as require() but they do it globally on all PHP scripts. The PHP auto_prepend_file and auto_append_file […]

    Reply

Leave a Reply Cancel reply

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

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.