Correct syntax for an include

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
User avatar
joneshy1
Registered User
Posts: 38
Joined: Wed Jan 05, 2022 10:57 pm
Location: Goose Creek, SC USA
Name: John Jones
Contact:

Correct syntax for an include

Post by joneshy1 »

I've seen several different ways to use the include statement...
1. include 'path to include file';
2. include "path to file";
3. include("path to file");

Now how about the actual path...
http://localhost/phpbb3/path/to/file.php
localhost/phpbb3/path/to/file.php
../phpbb3/path/to/file.php

and this...
<?php include statement ?>
or this
<!-- PHP --> include statement <!-- ENDPHP -->

Thanks
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 5871
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.
Contact:

Re: Correct syntax for an include

Post by thecoalman »

If you are referring to using PHP in templates you might want to consider not doing that for a lot of reasons. In any event I've not used this but you first need to enable it under Security settings. The path is relative to phpBB's root directory.

Code: Select all

<!-- INCLUDEPHP somefile.php --> 




To include file in phpBB php script that resides in phpBB's root:

Code: Select all

include($phpbb_root_path . 'filename.' . $phpEx);




phpBB_root_folder/yourfolder/yourfile.php.

Code: Select all

include($phpbb_root_path . 'yourfolder/yourfile.' . $phpEx);
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
User avatar
joneshy1
Registered User
Posts: 38
Joined: Wed Jan 05, 2022 10:57 pm
Location: Goose Creek, SC USA
Name: John Jones
Contact:

Re: Correct syntax for an include

Post by joneshy1 »

thanks for the info coalman. I've printed that out to keep in my notes. Thanks again.
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: Correct syntax for an include

Post by david63 »

I would also add that if you are including another file in a .php file then it is good practice to add a check that the file is not already present.

Something like

Code: Select all

if (!function_exists('some function in yourfile'))
{
	include($phpbb_root_path . 'yourfolder/yourfile.' . $phpEx);
}
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
User avatar
joneshy1
Registered User
Posts: 38
Joined: Wed Jan 05, 2022 10:57 pm
Location: Goose Creek, SC USA
Name: John Jones
Contact:

Re: Correct syntax for an include

Post by joneshy1 »

Thanks David63 for the info. got it in my notes also.
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 5871
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.
Contact:

Re: Correct syntax for an include

Post by thecoalman »

david63 wrote: Sat Jan 15, 2022 8:29 am I would also add that if you are including another file in a .php file then it is good practice to add a check that the file is not already present.

Something like

Code: Select all

if (!function_exists('some function in yourfile'))
{
	include($phpbb_root_path . 'yourfolder/yourfile.' . $phpEx);
}
That's good practice David but for my own use it would be unnecessary, I only have one file included with functions in common.php which makes them available everywhere. You may be unnecessarily loading functions but it's just easier than editing multiple phpBB files. You can also use require instead of include, if the file is not found it's fatal error and the script will stop execution which makes it easy to identify if the path is wrong.

Another good practice overall is prepend custom files with a consistent unique strings, xyz_functions.php, xyz_someotherfile.php., etc. This will avoid naming conflicts, you can apply this to everything whether it's php variables, function names, CSS rules, custom HTML elements, etc. Other than avoiding conflicts it's also helpful to easily find and identify them if you consistently use the same string.
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
Post Reply

Return to “phpBB Custom Coding”