Home >Backend Development >PHP Tutorial >Let me tell you why require_once() in PHP is not easy to use!

Let me tell you why require_once() in PHP is not easy to use!

藏色散人
藏色散人Original
2021-08-30 09:51:174701browse

In the previous article "Do you know how to use the die() and sleep() functions in PHP? " gives you a brief introduction to the use of die() and sleep() functions. Interested friends can learn about it~

This article will tell you why require_once() in PHP is not good use!

But before saying that it is not easy to use, let’s first take a look at the definition and usage of the require_once() function.

require_once() The function is a built-in function in PHP, when we want to introduce one PHP file into another file, for example, when we need to introduce it multiple times in a PHP script It is very useful when you have a file. It is used to check if the file has been included more than once, because if the file has already been included, it will ignore all the included content when running the script.

The syntax is "require_once('file name and path');"

require_once function only accepts one parameter 'file name and path', which is what we want File to include in PHP script. It is a string type parameter.

The return value is: if the called file is found, and if the file is already included, then the function will return the Boolean value True. If the file is not included, then the function will include the file and return True. However, if the called file is not found, a fatal error occurs, no output is shown, and execution stops, returning Boolean False.

Usage example of require_once() function in PHP:

File name: test.php

Code:

<?php 
// 文件内容
echo "欢迎来到PHP中文网!"; 
?>

File name: index.php

Code:

<?php 
  
// 包括文件
  
require_once(&#39;test.php&#39;); 
require_once(&#39;test.php&#39;); 
  
?>

Output:

欢迎来到PHP中文网!

Note: The require_once() function will ignore all similar imports after the first file is imported.

Let’s talk about why the require_once() function is not easy to use?

There are two points:

1. The require_once() function brings a lot of load to the server while including all files.

2. The function of require_once() function does not work properly when used in repeated functions when storing variables.

File name: my_file.php

Example:

<?php 
// 文件内容
$var = &#39;PHP&#39;; 
?>

File name: check.php

<?php
function func() {
    require_once(&#39;my_file.php&#39;);
    return $var;
}
for($i = 1; $i <= 3; $i++) {
    echo func() . "<br>";
}
?>

Output:

PHP

Passed By replacing the require_once() function in the above example with the require() function, we can ensure that the variable $var is available on every function call.

File name: check2.php

<?php
  
function func() {
    require(&#39;my_file.php&#39;);
    return $var;
}
  
for($i = 1; $i <= 3; $i++) {
    echo func() . "<br>";
}
  
?>

Output:

PHP PHP PHP

Compared with require() or include() function, require_once() function is slower because it Check whether the file is included every time the script calls the function.

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of Let me tell you why require_once() in PHP is not easy to use!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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