Home Backend Development PHP Tutorial 引入PHP包含文件重用PHP程序代码_PHP教程

引入PHP包含文件重用PHP程序代码_PHP教程

Jul 13, 2016 pm 05:30 PM
include php code content function Include introduce document hour use of program repeat Reuse page

当页面中再三重复用到同样的内容时,用include()函数包含文件,这个方法可以简化编码工作。例如,关联相同的数据库;在页面的页眉或页脚显示相同的HTML。类似的应用不胜枚举。那么如何来应用呢?

现在来看一下,在“example.inc”文件中,如果加入如下的代码: 就和把它们全部直接写到前面的代码中一样。唯一要注意的是必须把PHP分隔符(上述代码的首行和末行)也加入被包含的文件中,否则这些代码仅仅在那里显示而不被执行。实际上可以指定任何包含文件的名字,用.inc文件名是为了来把它们和其他PHP脚本中分隔开。也可以用下面方法实现, 这和上面的include()函数包含有什么区别呢?如果用require()函数包含的文件不存在,那么当运行脚本时程序就停止运行并给出错误信息。如果是用include包含的话,则显示一条警告,仍继续执行脚本。在我们下面的例子中,就是一个可能出现错误例子。用include()函数包含时候出现如下错误提示: Warning: Failed opening hello.inc for inclusion (include_path=) in /tutorial/9.php on line 6 如果用require()函数包含文件,可能出现以下错误信息: Fatal error: Failed opening required hello.inc (include_path=) in /tutorial/9.php on line 6 看到里边的细微差别了吗?用require包含文件的话,给出了一个“严重错误;而用include()函数包含文件只给出一个“警告”。 用包含文件唯一的问题是是否在包含文件中保存了密码。既然当连接数据库时,PostgreSQL 不用密码(至少本例中不用),那这个问题就不重要了。在以后的例子中,我们会用一个文件来包含所有信息。 [补充--by Boban] 1). 两者的区别是,require()函数包含进来的内容是作为整个文档的一个组成,而include()函数则不是。在一个正确的PHP程序中,使用require()函数的时候,如果包含进来的文件语法或者其他错误,则整个文档的PHP脚本都不再运行;如果使用include()函数包含的时候,程序只在有语法错误的地方提示错误,而PHP脚本的其他正确的部分继续运行。 2). include()函数可以使用在条件语句、循环控制语句中,例如if(1==1),来判断是否需要包含该文件,而require()函数将忽略这些条件,不管3721统统包含进来。 3). 如果包含文件中定义了变量、函数、类等,最好使用include()函数,并且做一个宏定义,例如if(!define("_EXAMPLE_INC")) define("_EXAMPLE_INC",1);语句防止重复包含而导致错误。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/509189.htmlTechArticle当页面中再三重复用到同样的内容时,用include()函数包含文件,这个方法可以简化编码工作。例如,关联相同的数据库;在页面的页眉或页...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles