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).
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).