PHP entry basics reference file study notes_PHP tutorial

WBOY
Release: 2016-07-13 17:16:29
Original
995 people have browsed it

There are certain differences between referencing files in PHP and referencing files in ASP. Let me introduce an example of using require and include to reference files in PHP.

Referring files is one of the special features of PHP. This method can put commonly used functions and functions in a file. When other pages need to use these functions or functions, they can directly reference this file. These functions are called. If it is not quoted, rewriting the same function on that page will greatly increase the developer's workload and increase the amount of code in the program, which is not conducive to later maintenance and secondary development.

There are two ways to reference files in PHP. The functions used are require() and include() respectively. The effect of the two references is the same, but there is a difference between the two functions: if require does not return any value when referencing the file, a fatal error will occur and the program will terminate and continue execution; when using this function to make a reference, you Make sure the code is used correctly. When include refers to a file, there is a return value, and it continues to execute the following code when an error occurs. Therefore, it is recommended that you try to use the first function require to reference the file. It returns no value and is relatively faster and more efficient than include. Usually require will be placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.

The code is as follows
 代码如下 复制代码

require('sql.php'); // 该函数通常放在开头,例如:引用SQL数据库连接函数的文件
echo '引用文件示范';
include('hello-world.php'); // 该函数一般是放在流程控制的处理部分中
?>

Copy code

 代码如下 复制代码

require_once('sql.php'); // 声明只引用sql.php文件一次
echo '引用文件示范';
include_once('hello-world.php'); //声明只引用hello-world.php文件一次
?>

​ require('sql.php'); // This function is usually placed at the beginning, for example: a file that references the SQL database connection function
echo 'Quote file demonstration';
Include('hello-world.php'); // This function is generally placed in the processing part of process control
?>

At this time, someone may ask, when a page references multiple files, and these referenced files also reference one or more other identical files, sometimes it is not necessary to reference so many times, then How can I make PHP only reference it once? Of course, PHP also has a corresponding method, which is to add a "suffix" statement to the original function, that is, change the functions into require_once() and include_once() respectively, as shown in the following example:

The code is as follows Copy code
​ require_once('sql.php'); // Declares that the sql.php file is only referenced once
echo 'Quote file demonstration';
Include_once('hello-world.php'); //Declare to only reference the hello-world.php file once
?> http://www.bkjia.com/PHPjc/628639.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628639.htmlTechArticleThere are certain differences between referencing files in php and asp. Let me introduce how to use require and ASP in php. include refers to the file instance. Reference files are one of the special features of PHP, this...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!