PHP Files

When using large pieces of PHP code, or you want to reuse the same piece of code in multiple places, it is wise to put this code in a separate PHP file.

For example, to include the PHP file located at yourdomain.com/myfiles/file.php, you can do:

{source}<?php
include JPATH_SITE.'/myfiles/file.php';
?>{/source}

Setting variables for a PHP File

If you want to set variables that are used in the PHP file, you can simply set them before the include/require, like:

{source}<?php
$name = 'Peter';
$surname = 'van Westen';
$interests = array( 'small fluffy things', 'green cantaloupe', 'toothpaste', '9V batteries' );
include JPATH_SITE.'/myfiles/file.php';
?>{/source}

Quick syntax PRO

With the Pro version of Sourcerer, you can include PHP files with an easier, shorter syntax. Simply use the php attribute (or file can also be used), and enter the path of the file:

{source php="myfiles/file.php"}{/source}

You can even enter multiple files at once, separating them with a comma:

{source php="myfiles/file1.php,myfiles/file2.php,myfiles/file3.php"}{/source}

And then you can, of course, also place additional PHP inside the tags.

For instance, if you set variables inside the PHP file, you can then use/manipulate them further in the PHP block:

{source php="myfiles/file.php"}<?php
   echo '<div class="mydiv">' . $var_from_file . '</div>';
?>{/source}

Include Method PRO

The method above will simply include the file.

But if you have a PHP file with classes and functions in it, you will get issues when including the file more than once on your page. In that case it is better to only make it include/require the file once.

When using the short syntax, you can also specify the Include Method you want to use for the PHP file.

To do this, instead of the generic php attribute (which equals to include), you can use one of the following four attributes to specify the Include Method:

{source include="myfiles/file.php"}{/source}
{source include_once="myfiles/file.php"}{/source}
{source require="myfiles/file.php"}{/source}
{source require_once="myfiles/file.php"}{/source}

With the Pro version, you can also use the Sourcerer editor button to help you enter a PHP file and easily select your desired Include Method.

Text or HTML Files

For text files, like .txt or .html you could use this syntax:

{source}<?php
echo file_get_contents( JPATH_SITE.'/myfiles/file.txt' );
?>{/source}

Dealing with Document structures

Please keep in mind that files you include SHOULD NOT generate their own html structure (<html>, <head>, <body> tags) inside your content.

When you include a file, it will be placed inside your Joomla content, which is already inside a full HTML structure. If you place structure tags in your content, you will get invalid HTML, which can cause all sorts of issues.

So if you want to load in html files, or when copying code from some ready-made html/script, make sure you only place the text part (what is inside the <body> tags) into your content.

If you include a PHP file, it should also not output any of these main html structure parts. Otherwise you will either have to use iframes or use some more advanced PHP code to strip the html structure away.

If you need to add CSS or JavaScript to the head of your page, you can do so via PHP. See above: How to add code to the head of the HTML page.