Table of Contents
简单谈谈PHP中的include、include_once、require以及require_once语句,requireonceinclude
Home php教程 php手册 简单谈谈PHP中的include、include_once、require以及require_once语句,requireonceinclude

简单谈谈PHP中的include、include_once、require以及require_once语句,requireonceinclude

Jun 13, 2016 am 08:40 AM
include once require

简单谈谈PHP中的include、include_once、require以及require_once语句,requireonceinclude

1.include语句

使用include语句可以告诉PHP提取特定的文件,并载入它的全部内容

<&#63;php
inlude "fileinfo.php";

//此处添加其他代码
&#63;>

Copy after login

2.include_once语句

每次使用include语句时,它都会重新将请求的文件导入,即使这个文件已经被导入过。例如,假定fileinfo.php文件包含许多函数,我们使用include语句将他导入到现有的文件中,然后我们又导入了一个包含fileinfo.php的文件,通过嵌套,我们已经将fileinfo.php文件导入了两次,这就会产生错误,因为我们试图多次定义同名的变量或函数。为了避免这样的事情发生,我们使用include_once语句来代替include语句

<&#63;php
include_once "fileinfo.php";

//此处添加其他代码
&#63;>

Copy after login

此时,如果在相同的文件中遇到另一个include或include_once语句时,PHP会检查它是否已经被导入过,如果是,就忽略它。

3.require和require_once语句

使用include和include_once语句的潜在问题是:PHP只会试图导入被请求导入的文件,即使该文件没有被找到,程序依旧会执行。
当我们绝对需要导入一个文件时,使用require语句,对于使用require_once语句的原因也是一样的,在这就不再赘述了。

<&#63;php
require_once "fileinfo.php";

//此处添加其他代码
&#63;>

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

vue3+vite: How to solve the error when using require to dynamically import images in src vue3+vite: How to solve the error when using require to dynamically import images in src May 21, 2023 pm 03:16 PM

vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

What is the difference between php include and include_once What is the difference between php include and include_once Mar 22, 2023 am 10:38 AM

When we write web pages using PHP, sometimes we need to include code from other PHP files in the current PHP file. At this time, you can use the include or include_once function to implement file inclusion. So, what is the difference between include and include_once?

What are the uses of require? What are the uses of require? Nov 27, 2023 am 10:03 AM

Usage of require: 1. Introduce modules: In many programming languages, require is used to introduce external modules or libraries so that the functions they provide can be used in the program. For example, in Ruby, you can use require to load third-party libraries or modules; 2. Import classes or methods: In some programming languages, require is used to import specific classes or methods so that they can be used in the current file; 3. Perform specific tasks: In some programming languages ​​or frameworks, require is used to perform specific tasks or functions.

Steps to solve the fatal error in the php header: require(): Failed opening required 'data/tdk.php' (include_path='.;C:\php\pear') Steps to solve the fatal error in the php header: require(): Failed opening required 'data/tdk.php' (include_path='.;C:\php\pear') Nov 27, 2023 pm 12:51 PM

Steps to resolve fatalerror:require():Failedopeningrequired'data/tdk.php'(include_path='.;C:phppear') in PHP header When developing websites or applications using PHP, we often encounter various errors . One of the common errors is "fatalerror:require():Failed

Steps to solve fatal error in php header: require(): Failed opening required 'data/tdk.php' Steps to solve fatal error in php header: require(): Failed opening required 'data/tdk.php' Nov 27, 2023 am 10:41 AM

Steps to resolve FatalError:require():Failedopeningrequired'data/tdk.php' in PHP header When developing and maintaining PHP websites, we often encounter various errors and exceptions. One of the common errors is "FatalError:require():Failedopeningrequired'data/tdk.php'".

How to use event modifier .once in Vue to achieve an event that only fires once How to use event modifier .once in Vue to achieve an event that only fires once Jun 11, 2023 pm 07:58 PM

Vue is a popular front-end framework that provides many convenient features, including an event system. Vue's event system allows developers to easily bind events and listen for events. Event modifiers are a feature in the Vue event system that are used to modify the behavior of events. In this article, we will introduce how to use the event modifier .once in Vue to achieve an event that is triggered only once. What are event modifiers? Event modifiers are a feature in the Vue event system that are used to modify the behavior of events. Vue provides some event modifiers

Detailed explanation of the role and usage of the require keyword in PHP Detailed explanation of the role and usage of the require keyword in PHP Jun 28, 2023 pm 11:31 PM

Detailed explanation of the role and usage of the require keyword in PHP In PHP development, require is a very commonly used keyword. Its function is to include the specified file for use by the current script. This article will explain in detail the function and use of the require keyword. 1. The role of the require keyword The require keyword can include the contents of a file into the current script. It is usually used to include some necessary external files, such as library files, configuration files, etc. Use req

How to include one php.ini file within another php.ini file? How to include one php.ini file within another php.ini file? Sep 02, 2023 pm 03:45 PM

Unable to include .ini file in main php,ini file. In contrast, when compiling PHP, the line --with-config-file-scan-dir=PATH​​​​​&a

See all articles