0% found this document useful (0 votes)
13 views2 pages

Include Vs Require PHP

In PHP, 'include' and 'require' are used to insert content from one file into another, with 'include' allowing execution to continue after an error, while 'require' stops execution if the file is missing. 'Include' is suitable for non-critical files, whereas 'require' is for essential files. Examples illustrate that 'include' will issue a warning if the file is missing, while 'require' will throw a fatal error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Include Vs Require PHP

In PHP, 'include' and 'require' are used to insert content from one file into another, with 'include' allowing execution to continue after an error, while 'require' stops execution if the file is missing. 'Include' is suitable for non-critical files, whereas 'require' is for essential files. Examples illustrate that 'include' will issue a warning if the file is missing, while 'require' will throw a fatal error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

include vs require in PHP

1. Introduction

In PHP, include and require are used to insert the content of one PHP file into another. This is useful for

reusing code like headers, footers, or configuration files.

2. Syntax

include 'filename.php';

require 'filename.php';

3. Key Differences

Feature | include | require

---------------|-----------------------------|-----------------------------

On Error | Throws a warning | Throws a fatal error

Execution | Continues after error | Stops execution

Usage | Non-critical files | Critical files

4. Example of include

// main.php

include 'header.php';

echo "Welcome to the site!";

If header.php is missing, PHP will throw a warning and continue executing main.php.

5. Example of require

// main.php

require 'config.php';

echo "Database connection established!";

If config.php is missing, PHP will throw a fatal error and stop execution.
include vs require in PHP

6. Tips

- Use include when the file is not mandatory.

- Use require when the file is essential for the application to run.

You might also like