hello,
What is the difference between include() function and require() function in PHP ?
Both the function seems to do the same job, so when to use what ?
If anyone has any ideas regarding this.........please help
hello,
What is the difference between include() function and require() function in PHP ?
Both the function seems to do the same job, so when to use what ?
If anyone has any ideas regarding this.........please help
You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function.
The two functions are identical in every way, except how they handle errors.
include() function - generates a warning (but the script will continue execution) while
require() function - generates a fatal error (and the script execution will stop after the error).
The include() statement includes and evaluates the specified file. This also applies to require().
The two constructs are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked for only in the current working directory or parent of the current working directory, respectively.
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
require() is identical to include() except upon failure it will produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.
The two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages.
This can save the developer a considerable amount of time. This means that you can create a standard header or menu file that you want all your web pages to include. When the header needs to be updated, you can only update this one include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all web pages).
Bookmarks