Home > Backend Development > PHP Tutorial > PHP uses custom function library include() and require() function usage detailed explanation

PHP uses custom function library include() and require() function usage detailed explanation

伊谢尔伦
Release: 2023-03-11 12:52:01
Original
1833 people have browsed it

Code reuse
Improve development efficiency and reduce costs by reusing existing code
include() and require() functions. [Interview Frequently Asked Questions]
require() imports a file during preprocessing, like pasting the file into where the function is used.

include() is almost equivalent to require(). The difference is that it is included when the script is executed. When the processing fails, include() generates a warning while require() results in a fatal error.

include_once( ) and require_once( ) functions
The two functions are included and included during script execution. Run the specified file. Similar to the include() statement and require(), the only difference is that if the code in the file is already included, it will not be included again, but only once. These two functions should be used when the same file may be included more than once during script execution, and you want to ensure that it is only included once to avoid problems with function redefinition, variable reassignment, etc.

<?php
    require &#39;config.php&#39;;           //使用require语句包含并执行config.php文件
    if ($condition)                     //在流程控制中使用include语句
        include &#39;file.txt&#39;;             //使用include语句包含并执行file.txt文件
    else                                    //条件不成立则包含下面的文件
        include (&#39;other.php&#39;);      //使用include语句包含并执行other.php文件
    require (&#39;somefile.txt&#39;);       //使用require语句包含并执行somefile.txt文件
Copy after login

Example:

<?php
error_reporting(0);  	
require "function.inc.php";
	if($a=="a") include "demo.txt";
	else include "demo2.html";
	one();
	two();
	three();
Copy after login

function.inc.php contains The purple one:

<?php
	function one(){
		echo "1111111<br>";
	}
	function two(){
		echo "222222<br>";
	}
	function three(){
		echo "333333<br>";
	}
Copy after login

The html contains the purple one:

bbbbbbbbbbbbbbbbbbbb

Output the purple one:

bbbbbbbbbbbbbbbbbbbb
1111111
222222
333333

## At the end of last year, the unsatisfactory .net homework and school competition ended. It's just unsatisfactory. It's far away from expectations, but it's still manageable. I relaxed a bit the day before yesterday and yesterday. Today, I spent another day sorting out some of the knowledge points on PHP functions. Mainly because of the half-way distance. Yue, I forgot too much. Then again, I didn’t learn it well the first time. As expected, just watching the video is not enough. I expect to be able to figure out anonymous functions and closures before 7 o’clock. Finished, I’m working on linux in the evening. I’m so happy when I think that I still have a day of my own time tomorrow~~Tomorrow should be linux+state compression

Looking back on the past year, it’s really fulfilling The overall state was so bad that I suddenly remembered that there was bestcoder at night, so I adjusted my state~~go~go~go~

When I went out on the ninth floor on the 30th, I was so scared. No, when I saw the teacher talking to other teachers at the door, I just moved out step by step. I didn’t want to feel this way again. T^T, my senior actually praised me when I went to bed (⊙﹏⊙)b What's so exciting about finishing ninth? I'm next year, no, this year! People who are looking for a job T^T, the content I have learned right now is not enough, not to mention that I can’t let go of the competition at all, and I obviously feel that I don’t have enough time.

Anyway, please keep working hard in the new year! !

The above is the detailed content of PHP uses custom function library include() and require() function usage detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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