Ok, another question for you all
We need to be able to include a certain range of forum id's in an IF/ELSE statement and need a better way of doing it than we're currently using.
Right now, we have 3 forum id's and are using:
PHP Code:
if ($fid == '1' OR $fid == '2')
{
show this
} else {
show that
}
We're possibly going to be using up to 10 forum id's for this and making multiple modifications through many files and need an easier way of updating this each time we add a forum id to the list.
I've not had any luck using in_array or array, probably because I am missing something, so I am hoping someone here will know the answer.
Any help would be greatly appreciated!
I would use a switch - it gives the best performance:
PHP Code:
switch($fid)
{
case 1:
case 2:
case 3:
...etc...
}
If you must use an array, in_array works fine, though I would prefer using array keys for larger arrays (or arrays searched multiple times) as it's much faster for PHP to search for a key than it is to perform a linear search on an array. Plus, isset() is a language construct, so you don't have the extra delay of an additional function call

Eg:
PHP Code:
$fids = array(1 => 1, 2 => 1, 3 => 1);
if(isset($fids[$fid]))
{
}
else
{
}
Thanks much
Only issue I am having now is to get it set in just one file so I don't have to replicate the same across multiple files. I've tried placing it in global.php though it's not really working.
A good deal of the modifications reside in functions_post.php, functions_forumlist.php, forumdisplay.php and showthread.php.
If you're placing it globally, and calling from within a function, make sure to reference the correct scope, either using using global directive, or using the $GLOABLS array, eg:
In global.php
PHP Code:
$mystuff = array(1, 2, 3);
In some function:
PHP Code:
function myfunction()
{
global $mystuff;
var_dump($mystuff); //works
}
function myfunction2()
{
var_dump($GLOBALS['mystuff']); // works too
}
Right now, we're not using a function, just the IF/ELSE referenced above, however, we use the same IF/ELSE multiple times, which is why we were looking to place it in global.php.
Two methods:
1) Place the code in a function - ie:
PHP Code:
function is_valid_forum()
...
2) Place a global switch - ie
PHP Code:
if(...)
$validForum = true;
else
$validForum = false;
ZiNga BuRgA Wrote:I would use a switch - it gives the best performance:
PHP Code:
switch($fid)
{
case 1:
case 2:
case 3:
...etc...
}
If you must use an array, in_array works fine, though I would prefer using array keys for larger arrays (or arrays searched multiple times) as it's much faster for PHP to search for a key than it is to perform a linear search on an array. Plus, isset() is a language construct, so you don't have the extra delay of an additional function call 
Eg:
PHP Code:
$fids = array(1 => 1, 2 => 1, 3 => 1);
if(isset($fids[$fid]))
{
}
else
{
}
Thank you very much, the forum keys actually worked out better in the end

.
Given we're going to be running 10-20 forum id's through the array, would their be a more efficient method? MyBB doesn't use $fid in all of the PHP files from what I've seen, it alternates between $fid and $forum['fid'].
We're moving between base php files, such as forumdisplay and showthread to the function_* files, which is why I ask.
Thanks again everyone!
For 10-20 IDs, efficiency should be a very minor factor, so long as you don't continually keep executing the same thing over again.
For grabbing an FID, this should work:
PHP Code:
global $foruminfo, $forum, $fid;
if(!isset($fid))
{
if(isset($foruminfo['fid']))
$fid = $foruminfo['fid'];
elseif(isset($forum['fid']))
$fid = $forum['fid'];
else
$fid = 0; //not in a forum
}