Difference between revisions of "PHP include"

From Wiki @ Karl Jones dot com
Jump to: navigation, search
(Example)
 
Line 9: Line 9:
 
== Example ==
 
== Example ==
  
Here is a PHP page named <code>message.php</code>, which contains some text.
+
Here is a PHP page named <code>message.php</code>, which contains some text:
  
 
<pre>
 
<pre>

Latest revision as of 19: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.

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