Include And Require Statement In PHP

Both the PHP Include and Require statements are employed to include a section of PHP code initially written in another file.


The file name comes immediately after the include or require keyword when writing a path name. The path name does not need to be registered if the directory is the same. The ability to include files using the include or require statement is beneficial since it allows us to create the identical code in a single file and then includes it anywhere we need it.


The include or require statement can be used to include files of the HTML or PHP types.

Clarifying the Distinction Between an Include and a require Statement.

The include statement and the require statement are functionally equivalent except where neither can locate the given file.

Require Statement

If the specified file is unavailable, the "require" statement will throw a fatal error (E COMPILE ERROR), thus ending the current program execution.

Include Statement

In this case, PHP will issue a warning (E-WARNING) but will continue executing the script nonetheless.

How to choose: include or require statement.

This question has a pretty easy answer. Use the required statement when you want to include a critical file needed for the script to continue running.


This is because the require statement stops the script from running if can't find the file, and you will know that the file can't be found.


Use the include statement, however, if you want your script to execute correctly and provide the result to the user even if the included file is not found. You can accomplish it by using the include statement.

Include Statement Syntax.

The include statement syntax is given below.


include 'filename';

Require Statement Syntax.

The require statement syntax is given below.


require 'filename';

Why do we use Include or Require Statements?

In developing websites, it is usual practice to utilize the include or require statement to incorporate the website's header, footer, or menu into every website page.


Thus, if we want to alter the footer of our website, we need to make changes to the footer file. The changes will be reflected on all the pages that include that footer file.


Because of this, both include statements and require statements are incredibly valuable and commonly used in programming.