Including External Files in PHP

PHP code on computer screen

 Scott-Cartwright/Getty Images

01
of 03

Include and Require

PHP is capable of utilizing SSI to include an external file in the file being executed. Two commands that do this are INCLUDE () and REQUIRE (). The difference between them is that when placed within a false conditional statement, the INCLUDE is not pulled but the REQUIRE is pulled and ignored. This means that in a conditional statement, it is faster to use INCLUDE. These commands are phrased as follows:

INCLUDE 'http://www.yoursite.com/path/to/file.php' ;
//or
REQUIRE 'http://www.yoursite.com/path/to/file.php' ;

Some of the most common uses for these commands include holding variables that are used across multiple files or holding headers and footers. If an entire site's layout is housed in external files called with SSI, any changes to site design need only be made to these files and the entire site changes accordingly.

02
of 03

Pulling the File

First, create a file that will hold the variables. For this example, it is called "variables.php."

//variables.php 
$name = 'Loretta';
$age = '27';
?>

Use this code to include the "variables.php" file in the second file called "report.php."

//report.php 
include 'variables.php';​
// or you can use the full path; include 'http://www.yoursite.com/folder/folder2/variables.php';

print $name . " is my name and I am " . $age . " years old.";
?>

As you can see, the print command easily uses these variables. You can also call the include within a function, but the variables must be declared as GLOBAL  to use them outside the function.

";​
//The line below will work because $name is GLOBAL

print "I like my name, " . $name;
print "
";​
//The next line will NOT work because $age is NOT defined as global

print "I like being " . $age . " years old.";
?>
03
of 03

More SSI

The same commands can be used to include non-PHP files such as .html files or .txt files. First, change the variables.php file name to variables.txt and see what happens when it is called.

//variables.txt 

 $name = 'Loretta'; 

 $age = '27'; 

 ?> 

 //report.php 

 include 'variables.txt'; 

 // or you can use the full path; include 'http://www.yoursite.com/folder/folder2/variables.txt';

 print $name . " is my name and I am " . $age . " years old."; 

 ?> 

This works just fine. Basically, the server replaces the include ''; line with the code from the file, so it actually processes this:

 //report.php

//variables.txt $name = 'Loretta'; $age = '27'; 

// or you can use the full path; include 'http://www.yoursite.com/folder/folder2/variables.txt 

print $name . " is my name and I am " . $age . " years old."; ?>

It is important to note that even if you include a non.php file if your file contains PHP code you must have the tags, or it will not be processed as PHP. For example, our variables.txt file above included PHP tags. Try saving the file again without them and then run report.php:

 //variables.txt 

$name = 'Loretta';
$age = '27';

This does not work. Since you need the tags anyway, and any code in a .txt file can be viewed from a browser (.php code cannot) just name your files with the .php extension to begin with.

Format
mla apa chicago
Your Citation
Bradley, Angela. "Including External Files in PHP." ThoughtCo, Feb. 16, 2021, thoughtco.com/including-external-files-in-php-2693792. Bradley, Angela. (2021, February 16). Including External Files in PHP. Retrieved from https://www.thoughtco.com/including-external-files-in-php-2693792 Bradley, Angela. "Including External Files in PHP." ThoughtCo. https://www.thoughtco.com/including-external-files-in-php-2693792 (accessed March 29, 2024).