PHP中的include和require - wixy
1.include语句
使用include语句可以告诉PHP提取特定的文件,并载入它的全部内容
-
<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> ?>
Copier après la connexion
2.include_once语句
每次使用include语句时,它都会重新将请求的文件导入,即使这个文件已经被导入过。例如,假定fileinfo.php文件包含许多函数,我们使用include语句将他导入到现有的文件中,然后我们又导入了一个包含fileinfo.php的文件,通过嵌套,我们已经将fileinfo.php文件导入了两次,这就会产生错误,因为我们试图多次定义同名的变量或函数。为了避免这样的事情发生,我们使用include_once语句来代替include语句
-
<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> ?>
Copier après la connexion
此时,如果在相同的文件中遇到另一个include或include_once语句时,PHP会检查它是否已经被导入过,如果是,就忽略它。
3.require和require_once语句
使用include和include_once语句的潜在问题是:PHP只会试图导入被请求导入的文件,即使该文件没有被找到,程序依旧会执行。
当我们绝对需要导入一个文件时,使用require语句,对于使用require_once语句的原因也是一样的,在这就不再赘述了。
-
<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> ?>
Copier après la connexion
总的来说,我们应该坚持使用require_once语句。
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques mois
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
1 Il y a quelques mois
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Solution d'énigmes de coquille
2 Il y a quelques semaines
By DDD
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
1 Il y a quelques mois
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Commandes de chat et comment les utiliser
1 Il y a quelques mois
By 尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)