What is include file in php?

Home | About Us | Contact Us

The three files, "default.php", "about.php", and "contact.php" should all include the "menu.php" file. Here is the code in "default.php":

Welcome to my home page

Some text

If you look at the source code of the "default.php" in a browser, it will look something like this:


Home |
About Us | 
Contact Us

Welcome to my home page

Some text

And, of course, we would have to do the same thing for "about.php" and "contact.php". By using include files, you simply have to update the text in the "menu.php" file if you decide to rename or change the order of the links or add another web page to the site.

The require[] Function

The require[] function is identical to include[], except that it handles errors differently.

The include[] function generates a warning [but the script will continue execution] while the require[] function generates a fatal error [and the script execution will stop after the error].

If you include a file with the include[] function and an error occurs, you might get an error message like the one below.

PHP code:






Error message:

Warning: include[wrongFile.php] [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Warning: include[] [function.include]:
Failed opening 'wrongFile.php' for inclusion
[include_path='.;C:\php5\pear']
in C:\home\website\test.php on line 5
Hello World!

Notice that the echo statement is still executed! This is because a Warning does not stop the script execution.

Now, let's run the same example with the require[] function.

PHP code:






Error message:

Warning: require[wrongFile.php] [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Fatal error: require[] [function.require]:
Failed opening required 'wrongFile.php'
[include_path='.;C:\php5\pear']
in C:\home\website\test.php on line 5

The echo statement was not executed because the script execution stopped after the fatal error.

It is recommended to use the require[] function instead of include[], because scripts should not continue executing if files are missing or misnamed.


See why there are 20,000+ Ektron integrations worldwide.
Request an INSTANT DEMO or download a FREE TRIAL today.

What is include [] and require [] in PHP?

include[] Vs require[] The only difference is that the include[] statement generates a PHP alert but allows script execution to proceed if the file to be included cannot be found. At the same time, the require[] statement generates a fatal error and terminates the script.

What is the function include [] used for?

The Include[] function is used to put data of one PHP file into another PHP file. If errors occur then the include[] function produces a warning but does not stop the execution of the script i.e. the script will continue to execute. First of all we create a PHP file.

What are the two methods to include files in PHP?

It is very helpful to include files when you want to apply the same HTML or PHP code to multiple pages of a website." There are two ways to include file in PHP..
include 'filename ';.
include ['filename'];.

What is include path in PHP?

The PHP Include Path is a set of locations that is used for finding resources referenced by include/require statements. Note: Adding libraries or external projects elements to your project's Include Path will also make elements defined in these resources available as content assist options.

Bài mới nhất

Chủ Đề