再讲php中require(),include(),require_once()和include_once()他们的区别
引用文件的方法有两种:require 及 include。两种方式提供不同的使用弹性。
require 的使用方法如 require("MyRequireFile.php"); 。这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份。常用的函数,亦可以这个方法将它引入网页中。
include 使用方法如 include("MyIncludeFile.php"); 。这个函数一般是放在流程控制的处理部分中。PHP 程序网页在读到 include 的文件时,才将它读进来。这种方式,可以把程序执行时的流程简单化。
_once 后缀表示已加载的不加载
1.报错
include引入文件的时候,如果碰到错误,会给出提示,并继续运行下边的代码
require引入文件的时候,如果碰到错误,会给出提示,并停止运行下边的代码
注:在 PHP 4.3.5 之前,包含文件中的语法错误不会导致程序停止,但从此版本之后会。
2.条件引用
include()与require()的功能相同,用法上却有一些不同,include()是有条件包含函数,而require()则是无条件包含函数,
例如下面例子,如果变量$somg为真,则将包含文件somefile.php:
代码如下 | 复制代码 |
if($some){ include 'somefile.php'; } |
但无论$some取何值,下面的代码将把文件somefile.php包含进文件里:
代码如下 | 复制代码 |
if($something){ require 'somefile.php'; } |
下面的例子充分说明了这两个函数之间的不同
代码如下 | 复制代码 |
$i = 1; while ($i |
可以从以上这段代码中看出,每一次循环的时候,程序都将把同一个文件包含进去,很显然这不我们想要的,可以看出这段代码希望在每次循环时,
将不同的文件包含进来,如果要完成这个功能,只能使用函数include()
代码如下 | 复制代码 |
$i = 1; while ($i include "somefile.$i.php"; $i++; } |
3.require用相对路径的时候
当A引用B,而B又引用了其他文件C时,C的路径如果是相对路径,则是相对于A的路径,而不是相对于B的'
4.require_once() 语句在脚本执行期间包括并运行指定文件。此行为和 require() 语句类似,唯一区别是如果该文件中的代码已经被包括了,则不会再次包括。
include_once() 语句在脚本执行期间包括并运行指定文件。此行为和 include() 语句类似,唯一区别是如果该文件中的代码已经被包括了,则不会再次包括。如同此语句名字暗示的那样,只会包括一次。
5..文件引用方式
include有返回值,而require没有
代码如下 | 复制代码 |
$login = include(’test.php’); if(!empty($login)) { echo “文件包含成功”; } else { echo “文件包含失败”; } |
include()执行时需要引用的文件每次都要进行读取和评估,
require()执行时需要引用的文件只处理一次(实际上执行时需要引用的文件内容替换了require()语句)
可以看出若有包含这些指令之一的代码和可能执行多次的代码,则使用require()效率比较高,
若每次执行代码时相读取不同的文件或者有通过一组文件叠代的循环,就使用include(),
可以给想要包括的文件名设置变量,当参数为 include()时使用这个变量
代码如下 | 复制代码 |
|
总结
incluce在用到时加载
require在一开始就加载
_once后缀表示已加载的不加载
php系统在加载php程序时有一个伪编译过程,可使程序运行速度加快。但incluce的文档仍为解释执行
include的文件中出错了,主程序继续往下执行
require的文件出错了,主程序也停了
所以包含的文件出错对系统影响不大的话(如界面文件)就用include,否则用require
以下文档也适用于 require()。这两种结构除了在如何处理失败之外完全一样。include() 产生一个警告而 require() 则导致一个致命错误。换句话说,如果你想在遇到丢失文件时停止处理页面就用 require()。include() 就不是这样,脚本会继续运行。同时也要确认设置了合适的 include_path。
require()函数用给定文件的内容取代它本身,这一代替过程发生在PHP引擎编译代码期间,而不是在执行期间进行,它不象include()那样会首先进行计算。require()函数更多地用在静态元素中,而include()更多地用于动态元素中。与include_once()类似的是,require_once()将首先检查是否已经插入给定的代码,如果代码已经存在,就不再插入了。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

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



Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

How to hide the select element in jquery: 1. hide() method, introduce the jQuery library into the HTML page, you can use different selectors to hide the select element, the ID selector replaces the selectId with the ID of the select element you actually use; 2. css() method, use the ID selector to select the select element that needs to be hidden, use the css() method to set the display attribute to none, and replace selectId with the ID of the select element.

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Because select allows developers to wait for multiple file buffers at the same time, it can reduce IO waiting time and improve the IO efficiency of the process. The select() function is an IO multiplexing function that allows the program to monitor multiple file descriptors and wait for one or more of the monitored file descriptors to become "ready"; the so-called "ready" state is Refers to: the file descriptor is no longer blocked and can be used for certain types of IO operations, including readable, writable, and exceptions. select is a computer function located in the header file #include. This function is used to monitor file descriptor changes—reading, writing, or exceptions. 1. Introduction to the select function. The select function is an IO multiplexing function.

1. Keywords in SQL statements are not case-sensitive. SELECT is equivalent to SELECT, and FROM is equivalent to from. 2. To select all columns from the users table, you can use the symbol * to replace the column name. Syntax--this is a comment--query out [all] data from the [table] specified by FEOM. * means [all columns] SELECT*FROM--query out the specified data from the specified [table] from FROM Data of column name (field) SELECT column name FROM table name instance--Note: Use English commas to separate multiple columns selectusername, passwordfrom

Implementing SelectChannels through golang Performance optimization of Go concurrent programming In the Go language, it is very common to use goroutine and channel to implement concurrent programming. When dealing with multiple channels, we usually use select statements for multiplexing. However, in the case of large-scale concurrency, using select statements may cause performance degradation. In this article, we will introduce some implementations of select through golang

SelectChannels for Reliability and Robustness Using Golang Introduction to Concurrent Programming: In modern software development, concurrency has become a very important topic. Using concurrent programming can make programs more responsive, utilize computing resources more efficiently, and be better able to handle large-scale parallel computing tasks. Golang is a very powerful concurrent programming language. It provides a simple and effective way to implement concurrent programming through go coroutines and channel mechanisms.

How to use golang for SelectChannelsGo concurrent programming Go language is a language that is very suitable for concurrent programming, in which the channel (Channel) and the Select statement are two important elements to achieve concurrency. This article will introduce how to use golang's SelectChannels for concurrent programming and provide specific code examples. 1. The concept of channel Channel is used for communication and data between goroutines
