PHP Include files

WBOY
Release: 2016-07-29 08:57:09
Original
1407 people have browsed it

The include (or require) statement takes all text/code/tags present in the specified file and copies them into the file using the include statement.

Included files are useful if you need to reference the same PHP, HTML or text on multiple pages of your website.

PHP include and require statements

With include or require statements, the contents of a PHP file can be inserted into another PHP file (before the server executes it).

include and require statements are identical, except for the error handling aspect:

  • require generates a fatal error (E_COMPILE_ERROR) and stops the script
  • include only generates a warning (E_WARNING), and the script continues

So if you If you want to continue execution and output results to the user even if the included file is missing, use include. Otherwise, in frameworks, CMS, or complex PHP application programming, always use require to reference key files to the execution flow. This helps improve application security and integrity in the event that a critical file is accidentally lost.

Including files saves a lot of work. This means you can create standard header, footer or menu files for all pages. Then, when the header needs updating, you simply update the header include file.

Syntax

include 'filename';
Copy after login

or

require 'filename';
Copy after login

PHP include Example

Example 1

Suppose we have a standard footer file named "footer.php", like this:

<?php
echo "<p>Copyright ? 2006-" . date("Y") . " W3School.com.cn</p>";
?>
Copy after login

If you need to To reference this footer file in a page, please use the include statement:

<html>
<body>

<h1>欢迎访问我们的首页!</h1>
<p>一段文本。</p>
<p>一段文本。</p>
<?php include 'footer.php';?>

</body>
</html>
Copy after login

require_once (PHP 4, PHP 5, PHP 7):

require_once statement is exactly the same as the require statement, the only difference is PHP It will check whether the file has already been included, and if so, it will not be included again. See the documentation for include_once to understand what _once means and how it differs from without _once.


The above introduces the PHP Include file, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template