Science, Tech, Math › Computer Science Including External Files in PHP Print Scott-Cartwright/Getty Images Computer Science PHP Programming Tutorials MySQL Commands Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Angela Bradley Angela Bradley Computer Science Expert B.A, History, Eastern Oregon University Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms. Learn about our Editorial Process Updated on May 02, 2019 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."; ?> Read More How to Include HTML in Many Documents Using PHP By Jennifer Kyrnin 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. Cite this Article 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 April 27, 2024). copy citation