We can include other files also in our PHP programs. Its for inserting the codes in other files in the current PHP program. Its a good practice to do make individual files with a piece of code which we need to use in several other pages of our project. Instead of writing the same codes in all pages we can include that file with the same code. Database connection code, reusable function and some global declarations , common header or footer or sidebar etc can be saved in a separate file , and we need to only include that file in the required page.
Here we are going to learn about various statements using for include a file.
There are mainly four statements are there for this in PHP
- include
- require
- include_ once
- require_ once
“include” and “require” statements will simply add the contents of the includes file.Its usage is like include ‘filename’; or require “filename”;( dont forget to provide the exact path to the file)
“include_ once” and “require_ once” statements will do the same job as include and require execpt it will include the file only once. Means even if we have tried to include the file same file more than once in a page using any of these statements it will include only once.
Now we can look at the different between these statements
Difference between include and require
Both will do the same job when including a file. Difference is occurring when the file is missing. include statement will produce only a warning (E_WARNING) and continue the execution of script . But require statement will produce a fatal level error (E_COMPILE_ERROR) and stops the execution of program.
Related Tutorials
PHP fwrite() – writing to a file
PHP fwrite() function is used for writing contents to a file. It write to the file stream pointed to by...
File Upload in PHP
It is very important to know how to upload a file in PHP.File upload in PHP can used for uploading...
PHP fread() – reading a file
PHP fread() function is used for reading contents of a file. It read from the file stream pointed to by...
PHP fopen() – opening, creating a file
PHP fopen() function is used for opening a file or url in PHP. It can also be used for creating...
PHP Switch case
PHP Switch case statement is an alternative for complex IF statements with a same expression. Its a best alternative for...
How a PHP program look like
PHP programs are saved in a file with extension “.php” ( we can use it in .html files also ,...
What is PHP and Full form of PHP ?
Full form of PHP is Hypertext Preprocessor PHP is the most popular and widely used server side scripting language for...
Break in PHP
“break” statement is using for end the current execution of loop control structure. Its applicable for for, foreach, while, do-while...
Can we include any type of files ? can I write PHP codes in a .txt file and include that file in another PHP file ?