通过Include和Require函数实现代码重用
include|函数
如果你做过各种大小的Web站点,一定会对重用代码段的重要性深有体会,不管是HTML还是PHP代码块。比如需要一年修改一次包含版权信息的页脚,而你有1000个Web页(就算是10个),手动操作则会让你痛苦不已。
通过PHP,你可以使用不同函数帮助你重用代码。具体用到的函数取决于你打算重用的内容。
主函数如下:
* include() and include_once()
* require() and require_once()
include()函数包含给定的文件,并对其进行检查。如:
include('/home/me/myfile');
在所含文件中的任何代码都将会在变量范围之内执行,此范围即等同于函数include()在父代码中出现的地方。你可以通过组合使用include()和fopen()函数以包含服务器上的静态文件或另一服务器上的目标文件。
include_once()函数与include()函数完成相同的功能,仅检查文件内的代码是否已经被当前脚本所包含过。如果代码已经被脚本所包含,该函数就不再包含相应的文件了。
require()函数使用给定文件的内容以替换自身。这一替换过程发生在PHP引擎编译你的代码时而非执行阶段,这与include()不同,后者首先被计算。require()函数更多的用于静态元素,而include()函数则主要用于动态元素。和include_once()类似,require_once()函数检查给定代码是否早已经插入到了文挡内,如果代码已经存在,给定代码就不会被再次插入文档。
建议对诸如版权、静态文本和其他不含变量的元素这类信息采用require函数,那些依赖其他脚本来实现自身内容的元素也应当采用require函数,例如:
[a lot of content]
//插入版权信息
require('/home/me/mycopyright');
?>
另一方面,常使用include()函数把函数库或类似库放到脚本之外:
//获取我的函数库
include('/home/me/myfunctions');
// 使用我的函数执行PHP程序?>
[a lot of content]
下一个自然而然的问题是:“被included或者被required的文件都来自哪里呢?”简单回答是:“你的系统。”不过,仅仅是一些代码片段有时会包含了诸如含确定用户名和密码的数据库连接之类的安全信息。显然你不希望这些东西也成为文档的一部分而被公之于众。
你可以把包含文件(included或者required)放在系统的任何地方,只要用户所处的PHP可以访问这些文件即可。你还可以给这些文件以任何你希望的扩展名,或者根本不设扩展名。
使用include()和require()函数可以让Web站点上普遍存在或者经常变动的元素具体化。而通过这一方式也会当需要升级时使系统体系结构更易于处理。

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



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

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

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?

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

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.

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.
