Home  >  Article  >  Backend Development  >  How to use include in php

How to use include in php

藏色散人
藏色散人Original
2019-10-21 09:29:5916181browse

How to use include in php

Usage of include in php

include (or require) statement will obtain all text present in the specified file /code/ tag and copied to 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

The include or require statements allow you to insert the contents of a PHP file into another PHP file (before the server executes it).

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

require generates a fatal error (E_COMPILE_ERROR) and stops the script

include generates only a warning (E_WARNING), and the script continues

So if you wish to continue execution and output results to the user even if the included file is missing, then 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';

or

require 'filename';

PHP include Example

Example 1

Suppose we have a name The standard footer file for "footer.php" is like this:

<?php
echo "<p>Copyright © 2006-" . date("Y") . " php.cn</p>";
?>
如需在一张页面中引用这个页脚文件,请使用 include 语句:
<html>
<body>
<h1>欢迎访问我们的首页!</h1>
<p>一段文本。</p>
<p>一段文本。</p>
<?php include &#39;footer.php&#39;;?>
</body>
</html>

Effect:

欢迎访问我的首页!
这是一个段落。
这是另一个段落。
Copyright © 2006-2019 php.cn

For more PHP related knowledge, please visit PHP Chinese website !

The above is the detailed content of How to use include in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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