-1

Today i have an interview and i am being asked by interviewer a question which is difference between require and include in php? I answered him as the difference between require and include is if you require a file that can not be loaded then it gives the fatal error and script will not execute and on the ohter hand if you include a file that can not be loaded it only gives the warning and continuous to the execution of script.

He replied: what is basic difference between these two instead of warning and fatal error? i couldn't justify my answer there.

when i came back i google it but nothing i found except this. Anyone knows what actually he wants to ask or listen from me? or any other difference?

DDay
  • 100
  • 1
  • 1
  • 9
  • It sounds like your interviewer mislead you by asking you "instead of" the warning and fatal error - since those are [the only differences in the two functions.](http://php.net/manual/en/function.require.php) Had he asked what the difference was in addition to, perhaps you would have accurately responded. – sjagr Dec 05 '14 at 06:04
  • 1
    http://stackoverflow.com/questions/3633900/difference-between-include-and-require-in-php – Bhavya Shaktawat Dec 05 '14 at 06:19

4 Answers4

5

1) If we add a file with this method and it does not exists at the given path, a Fatal Error will occur. *And the script will stop working *

2) If we add a file with this method and it does not exist at the given path, a Warning will occur. *And the script will not stop working *

There are different types of errors in PHP.

But with Parse Error and Fatal Error, scripts stop working.

With Parse Error (Syntax error e.g. missing ;), script will not run at all.

with Fatal Error, (e.g. including non-existing file with require, the script will execute till that line, but, not after that.)

Otherwise, they just show errors.

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • This is exactly which i answered to him but he said what is the basic difference ? i don't understand what he wants to listen form me? – DDay Dec 05 '14 at 06:03
  • @DDay I would've asked for a different interviewer - or just turned down the job. – sjagr Dec 05 '14 at 06:08
0

require will produce a fatal error (E_COMPILE_ERROR) and stop the script

include will only produce a warning (E_WARNING) and the script will continue

for more details check out this link

Difference between “include” and “require” in php

PHP File Inclusion

Community
  • 1
  • 1
Arun Yokesh
  • 1,346
  • 17
  • 19
0

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop the execution.

A warning is a message saying "You are doing something wrong and it is very likely to cause errors in the future, so please fix it."

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
-1

What you answered is 100% correct. According to w3schools:

The include and require statements are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue
Community
  • 1
  • 1
Muhammad Bilal
  • 2,106
  • 1
  • 15
  • 24