search
Homephp教程php手册PHP中文件包含语句的区别

  PHP中有四个包含文件的函数:include(), include_once(), require()和require_once()。弄清楚他们的区别是学习PHP的基础之一,可以避免不少写代码过程中产生的不必要的麻烦。

  include()

  1. 调用方式:include(“/path/to/filename”)

  2. 说明: include()语句将在它被调用的地方包含参数所指定的文件,其效果和将某个文件的内容复制在include()出现的地方一样。使用include()时,括号可以忽略,如:include “/path/to/filename”。

  3. 陷阱:通过if…else…条件语句来判断是否include某个文件时有一个怪现象。如

  

  if(expression)

  include("/path/to/filename");

  else

  include("/path/to/anotherfilename");

  ?>

  上面这段代码运行时可能出错。为什么呢?include()函数只是将文件内容复制到出现该include()函数的地方,如果文件中包含多行php语句而没有使用{}组成代码快呢?那整个if…else…的逻辑就乱了。所以,这段代码应该这样写:

  

  if(expression){

  include("/path/to/filename");

  }

  else{

  include("/path/to/anotherfilename");

  }

  ?>

  这样就可以确保所包含进来的文件在整个代码快中。

  include_once()

  1. 调用方式:include_once(“filename”)

  2. 说明:顾名思义,只包含一次该文件。即,如果上下文中已经包含过了该文件,那么就不再包含。

  3. 陷阱:拥有和include()函数一样陷阱。

  require()

  1. 调用方式:require(“filename”)

  2. 说明:除了以下两点之外,功能跟include()一样:(1)无论require()出现在程序片段的什么位置,它都能将文件包含进来。考虑如下程序:

  

  if(false){

  require("/path/to/filename");

  }

  else{

  require("/path/to/anotherfilename");

  }

  ?>

  上面语句将filename和anotherfilename两个文件都包含进来,即使第一个条件测试的条件为false。(2)require()出错时(如所require的文件不存在错误),php脚本程序将停止执行,但include()不会出现这种情况。

  3. 陷阱:拥有和include()一样的陷阱。

  require_once()

  1. 调用方式:require_once(“filename”)

  2. 说明:除了只包含一次某文件之外,其它功能和require()一样。

  3. 陷阱:拥有和require()一样的陷阱。



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 Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),