Heim php教程 php手册 PHP中的include和require - wixy

PHP中的include和require - wixy

May 20, 2016 am 11:39 AM

1.include语句


使用include语句可以告诉PHP提取特定的文件,并载入它的全部内容
  1. <span style="color: #008080;">1</span> <span style="color: #000000;">php
    </span><span style="color: #008080;">2</span> inlude "fileinfo.php"<span style="color: #000000;">;
    </span><span style="color: #008080;">3</span> 
    <span style="color: #008080;">4</span> <span style="color: #008000;">//</span><span style="color: #008000;">此处添加其他代码</span>
    <span style="color: #008080;">5</span> ?>
    Nach dem Login kopieren
 
 
2.include_once语句
 
每次使用include语句时,它都会重新将请求的文件导入,即使这个文件已经被导入过。例如,假定fileinfo.php文件包含许多函数,我们使用include语句将他导入到现有的文件中,然后我们又导入了一个包含fileinfo.php的文件,通过嵌套,我们已经将fileinfo.php文件导入了两次,这就会产生错误,因为我们试图多次定义同名的变量或函数。为了避免这样的事情发生,我们使用include_once语句来代替include语句
  1. <span style="color: #008080;">1</span> <span style="color: #000000;">php
    </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">include_once</span> "fileinfo.php"<span style="color: #000000;">;
    </span><span style="color: #008080;">3</span> 
    <span style="color: #008080;">4</span> <span style="color: #008000;">//</span><span style="color: #008000;">此处添加其他代码</span>
    <span style="color: #008080;">5</span> ?>
    Nach dem Login kopieren
此时,如果在相同的文件中遇到另一个include或include_once语句时,PHP会检查它是否已经被导入过,如果是,就忽略它。
 
 
3.require和require_once语句
 
使用include和include_once语句的潜在问题是:PHP只会试图导入被请求导入的文件,即使该文件没有被找到,程序依旧会执行。
当我们绝对需要导入一个文件时,使用require语句,对于使用require_once语句的原因也是一样的,在这就不再赘述了。
  1. <span style="color: #008080;">1</span> <span style="color: #000000;">php
    </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">require_once</span> "fileinfo.php"<span style="color: #000000;">;
    </span><span style="color: #008080;">3</span> 
    <span style="color: #008080;">4</span> <span style="color: #008000;">//</span><span style="color: #008000;">此处添加其他代码</span>
    <span style="color: #008080;">5</span> ?>
    Nach dem Login kopieren
 
总的来说,我们应该坚持使用require_once语句。
 
 
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)