File Inclusion in PHP
Introduction
-
Being able to include other files into our PHP scripts, is a useful thing.
-
The include( ) function allows us do this.
-
Suppose we have a text file that we want to include in a web page that we've already got up and running.
-
We could copy and paste the text from the file straight into our HTML. Or we could use the include( ) function
-
Including files saves a lot of work.
-
This means that we can create a standard header, footer, or menu file for all our web pages. Then, when the header needs to be updated, we can only update the header include file.
Syntax
<?php
include ('filename‘);
?>
Example
index.php
<?php
print "This is the original content<br>";
include (“secpage.php");
?>
Example
secpage.php
<?php
print "This is from second page";
?>
Output
Upon executing index.php, we'll get the following output:
This is the original content
This is from second page
This is because, to the web server, it sees the following content:
<?php
print "This is the original content<br>";
print "This is from second page";
?>