Difference between revisions of "PHP include"

From Wiki @ Karl Jones dot com
Jump to: navigation, search
Line 26: Line 26:
 
<body>
 
<body>
 
<?php
 
<?php
include message.php;
+
include 'message.php';
 
?>
 
?>
 
</body>
 
</body>
Line 33: Line 33:
  
 
In the above example, <code>some-page.php</code> includes the '''Hello World!''' text.
 
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 ==

Revision as of 19:16, 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.

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