Home > Backend Development > PHP Tutorial > PHP Advanced Tutorial (2): PHP Reference Files

PHP Advanced Tutorial (2): PHP Reference Files

黄舟
Release: 2023-03-03 15:38:01
Original
1318 people have browsed it

Server Side Invocation (SSI) is used to create functions, headers, footers or elements that can be reused across multiple pages.

Server Side Includes

With the include() or require() function, you can insert the contents of a file into the PHP file before the server executes the file. The two functions are identical except for the way they handle errors. The include() function generates a warning (but the script continues execution), while the require() function generates a fatal error (the script stops executing after the error occurs).

These two functions are used to create functions, headers, footers or elements that can be reused across multiple pages.

This will save developers a lot of time. This means you can create a standard header or menu file that is referenced by all web pages. When the header needs updating, you just update one include file, or when you add a new page to the site, you just need to modify the menu file (rather than updating the links in all pages).

include() function

include() function can get all the text in the specified file and copy the text to the file using the include function.

Example 1

设您拥有一个标准的页眉文件,名为 "header.php"。如需在页面中引用这个页眉文件,请使用 include() 函数,就像这样:
<html>
<body>

<?php include("header.php"); ?>

<h1>Welcome to my home page</h1>

<p>Some text</p>

</body>
</html>
Copy after login

Example 2

现在,假设我们有一个在所有页面上使用的标准菜单文件。请看下面这个 "menu.php":
<html>
<body>

<a href=http://www.phphtm.com>Home</a> |
<a href=http://www.phphtm.com>About Us</a> | 
<a href=http://www.phphtm.com>Contact Us</a>
三个文件,"default.php"、"about.php" 以及 "contact.php" 都引用了 "menu.php" 文件。这是 "default.php" 中的代码:
<?php include("menu.php"); ?>

<h1>Welcome to my home page</h1>

<p>Some text</p>

</body>
</html>
如果您在浏览器中查看 "default.php" 的源代码,应该类似这样:
<html>
<body>

<a href="default.php">Home</a> |
<a href="about.php">About Us</a> | 
<a href="contact.php">Contact Us</a>

<h1>Welcome to my home page</h1>
<p>Some text</p>

</body>
</html>
同时,当然,我们也将用相同的方法处理 "about.php" 和 "contact.php"。通过使用引用文件,在您需要重命名链接、更改链接顺序或向站点添加另一张网页时,只要简单地更新 "menu.php" 文件中的文本即可。
require() 函数
require() 函数与 include() 相同,不同的是它对错误的处理方式。
include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)。
如果在您通过 include() 引用文件时发生了错误,会得到类似下面这样的错误消息:
Copy after login

PHP Code:

<html>
<body>

<?php
include("wrongFile.php");
echo "Hello World!";
?>

</body>
</html>
Copy after login

Error message:

Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5

Warning: include() [function.include]:
Failed opening &#39;wrongFile.php&#39; for inclusion
(include_path=&#39;.;C:\php5\pear&#39;)
in C:\home\website\test.php on line 5

Hello World!
请注意,echo 语句依然被执行了!这是因为警告不会中止脚本的执行。
现在,让我们使用 require() 函数运行相同的例子。
Copy after login

PHP Code:

<html>
<body>

<?php
require("wrongFile.php");
echo "Hello World!";
?>

</body>
</html>
Copy after login

Error message:

Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5

Fatal error: require() [function.require]:
Failed opening required &#39;wrongFile.php&#39;
(include_path=&#39;.;C:\php5\pear&#39;)
in C:\home\website\test.php on line 5
Copy after login

Since the execution of the script was terminated after a fatal error, echo The statement will not be executed.

Because the script will not continue execution if the file does not exist or is renamed, we recommend using require() instead of include().

The above is the content of PHP Advanced Tutorial (2): PHP reference files. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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