require(),include(),require_once()和include_once()之间的区别
引用文件的方法有两种:require 及 include。
require 的使用方法如 require("file.php"); 。这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份。常用的函数,亦可以这个方法将它引入网页中。
include 使用方法如 include("file.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
require "somefile.$i.php";
$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()时使用这个变量
摘自 〃Style ひぐ

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

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

The C language function library is a toolbox containing various functions, which are organized in different library files. Adding a library requires specifying it through the compiler's command line options, for example, the GCC compiler uses the -l option followed by the abbreviation of the library name. If the library file is not under the default search path, you need to use the -L option to specify the library file path. Library can be divided into static libraries and dynamic libraries. Static libraries are directly linked to the program at compile time, while dynamic libraries are loaded at runtime.

In-depth discussion of the differences in console.log output in this article will analyze the reasons why the output results of console.log function in a piece of code are different. Code snippets involve URL parameter resolution...
