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> ?>
Copy after login
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> ?>
Copy after login
此时,如果在相同的文件中遇到另一个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> ?>
Copy after login
总的来说,我们应该坚持使用require_once语句。
Statement of this Website
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
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
1 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1375
52

