【PHP】界别include(),require(),include_once(),require_once()
【PHP】区分include(),require(),include_once(),require_once()
作者:zhanhailiang 日期:2013-03-19
1.include()与require()的区别(同理可区分include_once()与require_once())
include(),require()语句包含并运行指定文件。 这两种结构除了在如何处理失败之外完全一样。
include()产生一个警告而require()则导致一个致命错误。换句话说,如果想在遇到丢失文件时停止处理页面就用 require()。include()就不是这样,脚本会继续运行例1:include()产生一个Warning而require()则导致一个Fatal error。
<span style="color:rgb(51,51,51)">zhanhailiang<span class="sy0" style="color:rgb(102,204,102)">@</span>linux-06bq:~<span class="sy0" style="color:rgb(102,204,102)">></span> php <span class="re5">-r</span> <span class="st0" style="color:rgb(255,0,0)">"include('a.php');"</span> Warning: include<span class="br0" style="color:rgb(102,204,102)">(</span>a.php<span class="br0" style="color:rgb(102,204,102)">)</span>: failed to open stream: No such <span class="kw2" style="color:rgb(0,0,0); font-weight:bold">file</span> or directory <span class="kw1" style="color:rgb(177,177,0)">in</span> Command line code on line <span class="nu0" style="color:rgb(204,102,204)">1</span> </span><span style="color:#ff0000"><strong>Warning</strong></span><span style="color:#333333">: include</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">: Failed opening </span><span class="st_h" style="color:rgb(51,51,51)">'a.php'</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">for</span><span style="color:#333333"> inclusion </span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re2" style="color:rgb(0,0,255)">include_path</span><span style="color:#333333">=</span><span class="st_h" style="color:rgb(51,51,51)">'.:/usr/local/services/phplib/src:/usr/local/services/phplib/inc:/usr/local/services/php/lib/php'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> Command line code on line </span><span class="nu0" style="color:rgb(204,102,204)">1</span><span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">@</span><span style="color:#333333">linux-06bq:~</span><span class="sy0" style="color:rgb(102,204,102)">></span><span style="color:#333333"> php </span><span class="re5" style="color:rgb(51,51,51)">-r</span><span style="color:#333333"> </span><span class="st0" style="color:rgb(255,0,0)">"require('a.php');"</span><span style="color:#333333"> Warning: require</span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">a.php</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">: failed to open stream: No such </span><span class="kw2" style="color:rgb(0,0,0); font-weight:bold">file</span><span style="color:#333333"> or directory </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> Command line code on line </span><span class="nu0" style="color:rgb(204,102,204)">1</span><span style="color:#333333"> </span><strong><span style="color:#ff0000">Fatal error</span></strong><span style="color:#333333">: require</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">: Failed opening required </span><span class="st_h" style="color:rgb(51,51,51)">'a.php'</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re2" style="color:rgb(0,0,255)">include_path</span><span style="color:#333333">=</span><span class="st_h" style="color:rgb(51,51,51)">'.:/usr/local/services/phplib/src:/usr/local/services/phplib/inc:/usr/local/services/php/lib/php'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> Command line code on line </span><span class="nu0" style="color:rgb(204,102,204)">1</span>Copier après la connexion2.include()与include_once()的区别(同理可区别require()与require_once())
include_once()语句在脚本执行期间包含并运行指定文件。此行为和include()语句类似,唯一区别是如果该文件中的代码已经被包含了,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。
include_once()应该用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。
返回值和include()相同。如果文件已被包含,本函数返回TRUE。例1:include()会多次包含指定文件,而include_once()不会。
zhanhailiang<span class="sy0" style="color:rgb(102,204,102)">@</span>linux-06bq:~<span class="sy0" style="color:rgb(102,204,102)">></span> <span class="kw2" style="color:rgb(0,0,0); font-weight:bold">cat</span> a.php<span class="sy0" style="color:rgb(102,204,102)"><</span>?php<span class="kw3" style="color:rgb(0,0,102)">echo</span> <span class="st_h">'1'</span>.PHP_EOL;zhanhailiang<span class="sy0" style="color:rgb(102,204,102)">@</span>linux-06bq:~<span class="sy0" style="color:rgb(102,204,102)">></span> php <span class="re5">-r</span> <span class="st0" style="color:rgb(255,0,0)">"include('a.php');include('a.php');"</span><span class="nu0" style="color:rgb(204,102,204)">1</span><span class="nu0" style="color:rgb(204,102,204)">1</span>zhanhailiang<span class="sy0" style="color:rgb(102,204,102)">@</span>linux-06bq:~<span class="sy0" style="color:rgb(102,204,102)">></span> php <span class="re5">-r</span> <span class="st0" style="color:rgb(255,0,0)">"include_once('a.php');include_once('a.php');"</span><span class="nu0" style="color:rgb(204,102,204)">1</span>Copier après la connexion例2:include_once()避免函数重定义。
<span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">@</span><span style="color:#333333">linux-06bq:~</span><span class="sy0" style="color:rgb(102,204,102)">></span><span style="color:#333333"> </span><span class="kw2" style="color:rgb(0,0,0); font-weight:bold">cat</span><span style="color:#333333"> a.php </span><span class="sy0" style="color:rgb(102,204,102)"><</span><span style="color:#333333">?php</span><span class="kw3" style="color:rgb(0,0,102)">echo</span><span style="color:#333333"> </span><span class="st_h" style="color:rgb(51,51,51)">'1'</span><span style="color:#333333">.PHP_EOL; </span><span class="kw1" style="color:rgb(177,177,0)">function</span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">test</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">{</span><span class="br0" style="color:rgb(102,204,102)">}</span><span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">@</span><span style="color:#333333">linux-06bq:~</span><span class="sy0" style="color:rgb(102,204,102)">></span><span style="color:#333333"> php </span><span class="re5" style="color:rgb(51,51,51)">-r</span><span style="color:#333333"> </span><span class="st0" style="color:rgb(255,0,0)">"include('a.php');include('a.php');"</span><span style="color:#333333"></span><span class="nu0" style="color:rgb(204,102,204)">1</span><span style="color:#333333"> </span><strong><span style="color:#ff0000">Fatal error</span></strong><span style="color:#333333">: Cannot redeclare </span><span class="kw3" style="color:rgb(0,0,102)">test</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">previously declared </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">home</span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">a.php:</span><span class="nu0" style="color:rgb(204,102,204)">4</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">home</span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">a.php on line </span><span class="nu0" style="color:rgb(204,102,204)">4</span><span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">@</span><span style="color:#333333">linux-06bq:~</span><span class="sy0" style="color:rgb(102,204,102)">></span><span style="color:#333333"> php </span><span class="re5" style="color:rgb(51,51,51)">-r</span><span style="color:#333333"> </span><span class="st0" style="color:rgb(255,0,0)">"include_once('a.php');include_once('a.php');"</span><span style="color:#333333"></span><span class="nu0" style="color:rgb(204,102,204)">1</span>Copier après la connexion3.扩展阅读
include require include_once require_once 从php核心代码看require和include的区别Déclaration de ce site WebLe 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

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)

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

Visual Studio Code, également connu sous le nom de VS Code, est un éditeur de code source gratuit – ou environnement de développement intégré (IDE) – disponible pour tous les principaux systèmes d'exploitation. Avec une large collection d'extensions pour de nombreux langages de programmation, VS Code peut être c

Si vous êtes un développeur PHP expérimenté, vous aurez peut-être le sentiment d'y être déjà allé et de l'avoir déjà fait. Vous avez développé un nombre important d'applications, débogué des millions de lignes de code et peaufiné de nombreux scripts pour réaliser des opérations.

Ce tutoriel montre comment traiter efficacement les documents XML à l'aide de PHP. XML (Language de balisage extensible) est un langage de balisage basé sur le texte polyvalent conçu à la fois pour la lisibilité humaine et l'analyse de la machine. Il est couramment utilisé pour le stockage de données et

JWT est une norme ouverte basée sur JSON, utilisée pour transmettre en toute sécurité des informations entre les parties, principalement pour l'authentification de l'identité et l'échange d'informations. 1. JWT se compose de trois parties: en-tête, charge utile et signature. 2. Le principe de travail de JWT comprend trois étapes: la génération de JWT, la vérification de la charge utile JWT et l'analyse. 3. Lorsque vous utilisez JWT pour l'authentification en PHP, JWT peut être généré et vérifié, et les informations sur le rôle et l'autorisation des utilisateurs peuvent être incluses dans l'utilisation avancée. 4. Les erreurs courantes incluent une défaillance de vérification de signature, l'expiration des jetons et la charge utile surdimensionnée. Les compétences de débogage incluent l'utilisation des outils de débogage et de l'exploitation forestière. 5. L'optimisation des performances et les meilleures pratiques incluent l'utilisation des algorithmes de signature appropriés, la définition des périodes de validité raisonnablement,

Une chaîne est une séquence de caractères, y compris des lettres, des nombres et des symboles. Ce tutoriel apprendra à calculer le nombre de voyelles dans une chaîne donnée en PHP en utilisant différentes méthodes. Les voyelles en anglais sont a, e, i, o, u, et elles peuvent être en majuscules ou en minuscules. Qu'est-ce qu'une voyelle? Les voyelles sont des caractères alphabétiques qui représentent une prononciation spécifique. Il y a cinq voyelles en anglais, y compris les majuscules et les minuscules: a, e, i, o, u Exemple 1 Entrée: String = "TutorialSpoint" Sortie: 6 expliquer Les voyelles dans la chaîne "TutorialSpoint" sont u, o, i, a, o, i. Il y a 6 yuans au total

Liaison statique (statique: :) implémente la liaison statique tardive (LSB) dans PHP, permettant à des classes d'appel d'être référencées dans des contextes statiques plutôt que de définir des classes. 1) Le processus d'analyse est effectué au moment de l'exécution, 2) Recherchez la classe d'appel dans la relation de succession, 3) il peut apporter des frais généraux de performance.

Quelles sont les méthodes magiques de PHP? Les méthodes magiques de PHP incluent: 1. \ _ \ _ Construct, utilisé pour initialiser les objets; 2. \ _ \ _ Destruct, utilisé pour nettoyer les ressources; 3. \ _ \ _ Appel, gérer les appels de méthode inexistants; 4. \ _ \ _ GET, Implémentez l'accès à l'attribut dynamique; 5. \ _ \ _ SET, Implémentez les paramètres d'attribut dynamique. Ces méthodes sont automatiquement appelées dans certaines situations, améliorant la flexibilité et l'efficacité du code.
