PHP – Include/Require

  • Include and require statements are used to add file or other data into the currently executing program.
  • Include and require are perform same functions but there is slight difference between both these statements.
  • The main advantage is include/require statement is reduce the size of program and we can divide and save main program into several parts and execute all together.

Difference between include and require

So both these statements have more similarities than difference. But there is some difference between these two statements. The main difference shown when the program is in execution.

If there is no files kept in the specified directory and there is no include file is specified after include or require statements then,

  • In case of require statement it will produce fatal error and stop the execution of program.
  • In case of include statement it will produce only a warning message and continue the execution of  program.

Syntax 1 [ require ]

Syntax 2 [ include ]

 

Example 1 [ include ]

Output

if there is any file named as hello.php then execute that file and display it, Otherwise it shows warning messages and continue execution. (There is no file named as hello.php in my directory.)

 

Example 2 [ require ]

Output

if there is any file named as hello.php then execute that file and display it. Otherwise it shows error messages and stop execution. (There is no file named as hello.php in my directory.)

 

Leave a Reply