Home >Backend Development >PHP Problem >Several methods and characteristics of php include files

Several methods and characteristics of php include files

(*-*)浩
(*-*)浩Original
2019-09-16 14:31:302324browse

PHP include and require statements

Several methods and characteristics of php include files

In PHP, you can insert the contents of a file into the PHP file before it is executed by the server .

include and require statements are used to insert useful code written in other files into the execution flow.

In addition to the different ways of handling errors, include and require are the same in other aspects: (Recommended learning: PHP programming from entry to proficiency)

require generates a fatal error (E_COMPILE_ERROR), after which the script will stop executing.

include generates a warning (E_WARNING) and the script will continue execution after the error occurs.

So if you want 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 web pages. Then, when the header needs updating, you simply update the header include file.

Syntax

include 'filename';或者require 'filename';

PHP include and require statements

Basic examples

Suppose you have a standard header file named "header.php". To reference this header file in the page, please use include/require:

<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php include &#39;header.php&#39;; ?>
<h1>欢迎来到我的主页!</h1>
<p>一些文本。</p>
</body>
</html>

Example 2

Suppose we have a standard menu that is used in all pages document.

"menu.php":
echo &#39;<a href="/">主页</a>
<a href="/html">HTML 教程</a>
<a href="/php">PHP 教程</a>&#39;;

All pages in the website should reference this menu file. The following is the specific approach:

<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div class="leftmenu">
<?php include &#39;menu.php&#39;; ?></div>
<h1>欢迎来到我的主页!</h1>
<p>一些文本。</p>
</body>
</html>

Example 3

Assume we have an include file ("vars.php") that defines variables:

<?php
$color=&#39;red&#39;;$car=&#39;BMW&#39;;?>

These variables can be used in the calling file:

<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>欢迎来到我的主页!</h1>
<?php 
include &#39;vars.php&#39;;echo "I have a $color $car"; // I have a red BMW
?></body>
</html>

The above is the detailed content of Several methods and characteristics of php include files. 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