 |
Если вы впервые на нашем форуме - ознакомьтесь с правилами
|
|
Из книги Core PHP Programming 3rd Edition: "When the require function is processed, it is replaced with the file it points to. The include function acts more like a function call. The difference is most dramatic inside a loop. Imagine having three files you wanted to execute one after the other. You could put an include inside a for loop, and if the files were named something like include1.php, include2.php, and include3.php, you would have no problem. You could just build the name based on a counter variable.
If you used require, however, you would execute the first file three times. That's because on the first time through the loop, the call to require would be replaced with the contents of the file. As I said, the difference is subtle but can be very dramatic."
Имеем код:
test.php:
<?php
for($i=1;$i<=4;$i++){
require("PhpDocument$i.php");
}
?>
PhpDocument1.php:
<?
echo "1";
?>
PhpDocument2.php:
<?
echo "2";
?>
PhpDocument3.php:
<?
echo "3";
?>
PhpDocument4.php:
<?
echo "4";
?>
Итого на выходе получаем 1234, а не то что следует из книги... Мои предположения, что начиная с какойто версии php была изменена функциональность выражения require, так что оно работает как include, т.е. работает так как предпологается.А книга просто написана была по старой версии. Так ли это?
|
|
29 октября 2005, 16:02
|
|
Это сообщение было просмотрено: 5218 раз(а)
 |
 |
 |
 |
require в цикле | 5218 |
ArtG | 29.10.05 16:02 | |  Вполне возможно | 2913 |
Леонид Лукин | 30.10.05 07:01 | |  php.net | 3228 |
Medwoodu | 31.10.05 12:09 | |
|
 |