Difference between require() and include() in PHP [Archive] - Indian Web Development Forums - India's First Ever WebMaster Forums

PDA

View Full Version : Difference between require() and include() in PHP


varun
01-07-2006, 03:37 PM
Difference between "require()" and "include()" in PHP
This should be well known, and people should be aware as to why they are using either or. But, I've noticed lately that a lot of people new to PHP or programming are not aware of the difference. Depending on what you need you, need to decide what the differences are. So...

* require() : If the file does not exist, you will get a fatal error.
* include() : If the file does not exist, you will get a warning and the next line of code will execute.

Both functions interpret the file in HTML mode, so make sure that the code inside the included file starts and ends with the appropriate BEGIN and END tags (<?php ...... ?>)
Depending on how you do your error handling, or how you want your code to work, keep these things in mind before you blindly use require() or include() and wonder why something is not working the way you expected it to.

PS: Be warned that parse error in included file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does. (from php.net).

iWonder
01-07-2006, 03:56 PM
Difference between "require()" and "include()" in PHP
This should be well known, and people should be aware as to why they are using either or. But, I've noticed lately that a lot of people new to PHP or programming are not aware of the difference. Depending on what you need you, need to decide what the differences are. So...

* require() : If the file does not exist, you will get a fatal error.
* include() : If the file does not exist, you will get a warning and the next line of code will execute.

Both functions interpret the file in HTML mode, so make sure that the code inside the included file starts and ends with the appropriate BEGIN and END tags (<?php ...... ?>)
Depending on how you do your error handling, or how you want your code to work, keep these things in mind before you blindly use require() or include() and wonder why something is not working the way you expected it to.

PS: Be warned that parse error in included file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does. (from php.net).
Nice, Thanks ! :)

Cool_dude_Prav
01-08-2006, 07:37 PM
but normally most n00bs wud prefer using include as its more safe ;) AFAIK...

dannbkk
10-17-2008, 04:17 PM
In safer instances I prefer to use include