PHP include file - The php require function
In this tutorial I will show you how to divide your code into more files and how to include various files into your actual PHP code.
|Name:||PHP include file|
Bookmark PHP include file
Step 4 - The php require function
PHP include file
In a lot of cases it is very important to have the file really included in our code. For example if the include of a database management code fails and the script continue it's run then it will cause a lot of unwanted error in the later steps. To avoid this you can say if the include was not success then stop all the further activity. For this purpose was the function require() introduced.
The usage if php require function is quite similar to the include however if the file can not be found require will report a fatal error and the script will die.
Let's see an example:
Output:Try to include wrong file first with include.
Warning: include(internalwrong.php) [function.include]: failed to open stream: No such file or directory in Z:WorkWebSitestestf1test.php on line 3
Warning: include() [function.include]: Failed opening 'internalwrong.php' for inclusion (include_path='D:Program FilesZendZendStudio-5.5.0binZendFrameworklibrary') in Z:WorkWebSitestestf1test.php on line 3
Now try it with require.
Warning: require(internalwrong.php) [function.require]: failed to open stream: No such file or directory in Z:WorkWebSitestestf1test.php on line 5
Fatal error: require() [function.require]: Failed opening required 'internalwrong.php' (include_path='D:Program FilesZendZendStudio-5.5.0binZendFrameworklibrary') in Z:WorkWebSitestestf1test.php on line 5
Previous Step of PHP include fileNext Step of PHP include file
Tags: php incluude file, php include, include file, file include, php, include, file
|PHP include file - Table of contents|
|Step 1 - Include files introduction|
|Step 2 - The php include function|
|Step 3 - Include file more than once|
|Step 4 - The php require function|
|Step 5 - Include files only once|