Difference between revisions of "PHP include"
Karl Jones (Talk | contribs) (Created page with "In PHP, the '''include''' and '''require''' keywords copies the contents of one file into other file. This is useful for making a file (such as the header section of a we...") |
Karl Jones (Talk | contribs) (→Example) |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
This is useful for making a file (such as the header section of a web page) which is included into many web pages. | This is useful for making a file (such as the header section of a web page) which is included into many web pages. | ||
| − | include is optional: if the specified file does not exist, PHP ignores the include. | + | '''include''' is optional: if the specified file does not exist, PHP ignores the include. |
| − | require is not optional: if the specified file does not exist, PHP generates an error. | + | '''require''' is not optional: if the specified file does not exist, PHP generates an error. |
| + | |||
| + | == Example == | ||
| + | |||
| + | Here is a PHP page named <code>message.php</code>, which contains some text: | ||
| + | |||
| + | <pre> | ||
| + | Hello World! | ||
| + | </pre> | ||
| + | |||
| + | Here is a PHP page named <code>some-page.php</code>: | ||
| + | |||
| + | <pre> | ||
| + | <!DOCTYPE html> | ||
| + | <html> | ||
| + | <head> | ||
| + | <meta charset="utf-8"/> | ||
| + | <title>HTML 5 example: minimum required elements</title> | ||
| + | </head> | ||
| + | <body> | ||
| + | <?php | ||
| + | include 'message.php'; | ||
| + | ?> | ||
| + | </body> | ||
| + | </html> | ||
| + | </pre> | ||
| + | |||
| + | In the above example, <code>some-page.php</code> includes the '''Hello World!''' text. | ||
| + | |||
| + | == Syntax == | ||
| + | |||
| + | The word include (or require) followed by the filename (in quotation marks, either single or double). | ||
| + | |||
| + | If the included file is in a different folder from the file containing the include statement, then the path to the file must be included: | ||
| + | |||
| + | <pre> | ||
| + | <?php | ||
| + | include '../message.php'; | ||
| + | ?> | ||
| + | </pre> | ||
== See also == | == See also == | ||
Latest revision as of 20:40, 20 September 2016
In PHP, the include and require keywords copies the contents of one file into other file.
This is useful for making a file (such as the header section of a web page) which is included into many web pages.
include is optional: if the specified file does not exist, PHP ignores the include.
require is not optional: if the specified file does not exist, PHP generates an error.
Contents
Example
Here is a PHP page named message.php, which contains some text:
Hello World!
Here is a PHP page named some-page.php:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>HTML 5 example: minimum required elements</title> </head> <body> <?php include 'message.php'; ?> </body> </html>
In the above example, some-page.php includes the Hello World! text.
Syntax
The word include (or require) followed by the filename (in quotation marks, either single or double).
If the included file is in a different folder from the file containing the include statement, then the path to the file must be included:
<?php include '../message.php'; ?>
See also
External links
- PHP include @ php.net
- PHP include files @ w3schools.com