Книга: Fedora™ Unleashed, 2008 edition
Including Other Files
Including Other Files
Unless you are restricting yourself to the simplest programming ventures, you will want to share code among your scripts at some point. The most basic need for this is to have a standard header and footer for your website, with only the body content changing. However, you might also find yourself with a small set of custom functions you use frequently, and it would be an incredibly bad move to simply copy and paste the functions into each of the scripts that use them.
The most common way to include other files is with the include
keyword. Save this script as include1.php
:
<?php
for($i = 10; $i >= 0; $i -= 1) {
include "echo_i.php";
}
?>
Then save this script as echo_i.php
:
<?php
echo $i;
?>
If you run include1.php
, PHP loops from 10 to 0 and includes echo_i.php
each time. For its part, echo_i.php
just prints the value of $i
, which is a crazy way of performing an otherwise simple operation, but it does demonstrate how included files share data. Note that the include
keyword in include1.php
is inside a PHP block, but PHP is reopened inside echo_i.php
. This is important because PHP exits PHP mode for each new file, so you always have a consistent entry point.
- Other Operators
- 7.5. Other Bootloaders
- 9.9. Other File Systems
- 12.8.9 Renaming Version-Controlled Files
- Appendix E. Other resources and links
- Other debugging tools
- Для чего нужны папки Windows, Documents and Settings, Program Files и Temp?
- Можно ли указать использование по умолчанию вместо C:Program Files другого каталога для установки программ?
- Можно ли удалять из папки Program Files папки деинсталлированных программ?
- Use and Edit Files in the
- Абстрактный базовый класс FileSystemInfo
- Класс FileStream и байтовый ввод-вывод в файл