search
Homephp教程php手册php关于require和include的区别

include() 或 require() 函数,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容,除了它们处理错误的方式不同之外,这两个函数在其他方面都是相同的.

include() 或 require() 函数,您可以在服务器执行 php 文件之前在该文件中插入一个文件的内容,除了它们处理错误的方式不同之外,这两个函数在其他方面都是相同的,include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行).

<html> 
<body> 
<?php include("header.php");  
<h1 id="welcome-nbsp-to-nbsp-my-nbsp-home-nbsp-page">welcome to my home page</h1> 
<p>some text</p> 
</body> 
</html>

三个文件,"default.php"、"about.php" 以及 "contact.php" 都引用了 "menu.php" 文件,这是 "default.php" 中的代码:

<?php include("menu.php");  
<h1 id="welcome-nbsp-to-nbsp-my-nbsp-home-nbsp-page">welcome to my home page</h1> 
<p>some text</p> 
</body> 
</html>

require() 函数

require() 函数与 include() 相同,不同的是它对错误的处理方式.

include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行).

如果在您通过 include() 引用文件时发生了错误,会得到类似下面这样的错误消息:

<html> 
<body> 
<?php 
include("wrongfile.php"); 
echo "hello world!"; 
 
</body> 
</html>

错误消息:

warning: include(wrongfile.php) [function.include]:
failed to open stream:
no such file or directory in c:homewebsitetest.php on line 5
 
warning: include() [function.include]:
failed opening &#39;wrongfile.php&#39; for inclusion
(include_path=&#39;.;c:php5pear&#39;)
in c:homewebsitetest.php on line 5
 
hello world!


教程地址:

欢迎转载!但请带上文章地址^^

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version