Home Backend Development PHP Tutorial 通过Include和Require函数实现代码重用_PHP

通过Include和Require函数实现代码重用_PHP

Jun 01, 2016 pm 12:31 PM
include code function accomplish document pass

  如果你做过各种大小的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函数,例如:

  

  

Something

  

  [a lot of content]

  

  //插入版权信息

  require('/home/me/mycopyright');

  ?>

  

  

  另一方面,常使用include()函数把函数库或类似库放到脚本之外:

  

  //获取我的函数库

  include('/home/me/myfunctions');

  // 使用我的函数执行PHP程序?>

  

  

Something

  

  [a lot of content]

  

  

  下一个自然而然的问题是:“被included或者被required的文件都来自哪里呢?”简单回答是:“你的系统。”不过,仅仅是一些代码片段有时会包含了诸如含确定用户名和密码的数据库连接之类的安全信息。显然你不希望这些东西也成为文档的一部分而被公之于众。

  你可以把包含文件(included或者required)放在系统的任何地方,只要用户所处的PHP可以访问这些文件即可。你还可以给这些文件以任何你希望的扩展名,或者根本不设扩展名。

  使用include()和require()函数可以让Web站点上普遍存在或者经常变动的元素具体化。而通过这一方式也会当需要升级时使系统体系结构更易于处理。


  本文Julie Meloni 是i2i Interactive的技术主管,及Linux和开放源代码社区的大力推动者。她撰写了很多关于PHP及其他技术的书籍,并成为对CNET Builder.com具有长期贡献的专家。

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

Video Face Swap

Video Face Swap

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

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)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Tsinghua University and Zhipu AI open source GLM-4: launching a new revolution in natural language processing Jun 12, 2024 pm 08:38 PM

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

See all articles