The difference between include and require in PHP
The main focus is on the red tag statement.
The include (or require) statement takes all the Text/code/markup this exists in the specified file and copies it into the File that uses the include statement.
Including files is very useful if you want to include the same PHP, HTML, or text on multiple pages of a website.
PHP include and Require statements
It is possible to insert the content of a PHP file into another PHP file (before the server executes it), with the INCLU De or require statement.
The Include and require statements are identical, except upon failure:
Require would produce a fatal error (E_COMPILE_ERROR) and stop the scriptinclude would only produce a warning (e_warning) an d The script would continue
So, if you want the execution-go on and show users the output, even if the include file is missing, use the include STA Tement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the Require statement to include a K EY file to the flow of execution. This would help avoid compromising your application's security and integrity, just in-case one key file is accidentally mis Sing.
Including files saves a lot of work. This means the can create a standard header, footer, or the menu file for all your Web pages. Then if the header needs to is updated, you can only update the header include file.
Syntax
Include '
filename';
Or
Require '
filename';
PHP include Examples
Example 1
Assume we have a standard footer file called "footer.php", which is looks like this:
echo "
Copyright©1999-". Date ("Y"). "W3schools.com
";
?>
To include the footer file with a page, use the Include statement:
Example
Welcome to my home page!
Some text.
Some more text.
Run example?
Example 2
Assume we have a standard menu file called "menu.php":
Echo ' Home-
HTML Tutorial-
CSS Tutorial-
JavaScript Tutorial-
PHP Tutorial ';
?>
All pages of the WEB site should use this menu file. Here's how it can-be-done (we have using a element so, the menu easily can be styled with CSS later):
Example
Welcome to my home page!
Some text.
Some more text.
Run example?
Example 3
Assume we have a file called "vars.php" with some variables defined:
$color = ' red ';
$car = ' BMW ';
?>
Then, if we include the "vars.php" file, the variables can is used in the calling file:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example?
PHP include vs. require
The Require statement is also used to include a file into the PHP code.
However, there is one big difference between include and require; When a file was included with the include statement and PHP cannot find it, the script would continue to execute:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example?
If We do the same example using the require statement, the Echo statement would not be executed because the script Execution dies after the Require statement returned a fatal error:
Example
Welcome to my home page!
echo "I have a $color $car.";
?>
Run example?
|
Use require when the file was required by the application.
Use include when the file was not required and application should continue when file was not found. |
http://www.bkjia.com/PHPjc/845436.html www.bkjia.com true http://www.bkjia.com/PHPjc/845436.html techarticle The difference between include and require in PHP focuses on the red tag statement. The include (or require) statement takes all the Text/code/markup this exists in the specified file and ...
-