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:
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.Syntaxinclude ' filename‘;
Assume we have a standard footer file called "footer.php", which is looks like this:<?php
To include the footer file with a page, use the Include statement:Example<body>
Assume we have a standard menu file called "menu.php":<?php
All pages of the WEB site should use this menu file. Here's how it can do (we are using a <div> element so, the menu easily can be styled with CSS later):Example<body>
Assume we have a file called "vars.php" with some variables defined:<?php
Then, if we include the "vars.php" file, the variables can is used in the calling file:Example<body>
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<body>
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<body>
|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.