如果你做过各种大小的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站点上普遍存在或者经常变动的元素具体化。而通过这一方式也会当需要升级时使系统体系结构更易于处理。
本文Julie Meloni 是i2i Interactive的技术主管,及Linux和开放源代码社区的大力推动者。她撰写了很多关于PHP及其他技术的书籍,并成为对CNET Builder.com具有长期贡献的专家。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use