xs2 php include

Include php files in templates

Submitted by CyberAlien, Mar 2007.
This short tutorial explains how to include php files in phpBB templates.



To include php files use usual php include(), include_once(), require() or require_once() function. Path to included file must be relative to phpBB root directory, not to tpl file. Use variable $phpbb_root_path to make sure path is always relative to phpBB root directory.



Example:
Code:
  1. include($phpbb_root_path ‘includes/my_file.php’);
     
  2. include(‘myfile.php’); 

Also note that template is included inside a funciton, so if you are using variables inside your php file and you have some functions that use those variables using “global” directive, you must declate those variables as “global” too. Example of valid included php code:

Code:
  1. // declare variable as global so it could be accessed from change_var()
     
  2. global $myvar;
     
  3. function change_var()
     
  4. {
     
  5.  // declare variable as global to access previously declared variable $myvar
     
  6.  global $myvar;
     
  7.  $myvar = 1;
     
  8. } 

Share this tutorial

If you want to show this tutorial to someone else please use following code to link to this tutorial:

HTML Link:
Include php files in templates” class=”text” onclick=”this.focus(); this.select();”/>
Forum BBCode:

Content of this tutorial may not be published anywhere else without author’s permission. Please link to this page instead.