PHP Advanced Tutorial (2): PHP Reference Files
Server Side Invocation (SSI) is used to create functions, headers, footers or elements that can be reused across multiple pages.
Server Side Includes
With the include() or require() function, you can insert the contents of a file into the PHP file before the server executes the file. The two functions are identical except for the way they handle errors. The include() function generates a warning (but the script continues execution), while the require() function generates a fatal error (the script stops executing after the error occurs).
These two functions are used to create functions, headers, footers or elements that can be reused across multiple pages.
This will save developers a lot of time. This means you can create a standard header or menu file that is referenced by all web pages. When the header needs updating, you just update one include file, or when you add a new page to the site, you just need to modify the menu file (rather than updating the links in all pages).
include() function
include() function can get all the text in the specified file and copy the text to the file using the include function.
Example 1
设您拥有一个标准的页眉文件,名为 "header.php"。如需在页面中引用这个页眉文件,请使用 include() 函数,就像这样: <html> <body> <?php include("header.php"); ?> <h1>Welcome to my home page</h1> <p>Some text</p> </body> </html>
Example 2
现在,假设我们有一个在所有页面上使用的标准菜单文件。请看下面这个 "menu.php": <html> <body> <a href=http://www.phphtm.com>Home</a> | <a href=http://www.phphtm.com>About Us</a> | <a href=http://www.phphtm.com>Contact Us</a> 三个文件,"default.php"、"about.php" 以及 "contact.php" 都引用了 "menu.php" 文件。这是 "default.php" 中的代码: <?php include("menu.php"); ?> <h1>Welcome to my home page</h1> <p>Some text</p> </body> </html> 如果您在浏览器中查看 "default.php" 的源代码,应该类似这样: <html> <body> <a href="default.php">Home</a> | <a href="about.php">About Us</a> | <a href="contact.php">Contact Us</a> <h1>Welcome to my home page</h1> <p>Some text</p> </body> </html> 同时,当然,我们也将用相同的方法处理 "about.php" 和 "contact.php"。通过使用引用文件,在您需要重命名链接、更改链接顺序或向站点添加另一张网页时,只要简单地更新 "menu.php" 文件中的文本即可。 require() 函数 require() 函数与 include() 相同,不同的是它对错误的处理方式。 include() 函数会生成一个警告(但是脚本会继续执行),而 require() 函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)。 如果在您通过 include() 引用文件时发生了错误,会得到类似下面这样的错误消息:
PHP Code:
<html> <body> <?php include("wrongFile.php"); echo "Hello World!"; ?> </body> </html>
Error message:
Warning: include(wrongFile.php) [function.include]: failed to open stream: No such file or directory in C:\home\website\test.php on line 5 Warning: include() [function.include]: Failed opening 'wrongFile.php' for inclusion (include_path='.;C:\php5\pear') in C:\home\website\test.php on line 5 Hello World! 请注意,echo 语句依然被执行了!这是因为警告不会中止脚本的执行。 现在,让我们使用 require() 函数运行相同的例子。
PHP Code:
<html> <body> <?php require("wrongFile.php"); echo "Hello World!"; ?> </body> </html>
Error message:
Warning: require(wrongFile.php) [function.require]: failed to open stream: No such file or directory in C:\home\website\test.php on line 5 Fatal error: require() [function.require]: Failed opening required 'wrongFile.php' (include_path='.;C:\php5\pear') in C:\home\website\test.php on line 5
Since the execution of the script was terminated after a fatal error, echo The statement will not be executed.
Because the script will not continue execution if the file does not exist or is renamed, we recommend using require() instead of include().
The above is the content of PHP Advanced Tutorial (2): PHP reference files. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
