Using in_array() or array()
#1
Ok, another question for you all Smile

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:

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!
- Jon T. & Kim B.
- AirborneFive Co-Owner's
- Optimized Web Hosting Solutions | Starting @ Only $2/mo
- First Month of Service Free! | Coupon: FIRSTMONTHFREE
Reply
#2
$fids = array('x','y','z');

if(in_array($fid, $fids))
{
...
}
else
{
...
}
Reply
#3
I would use a switch - it gives the best performance:
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 Toungue
Eg:
$fids = array(1 => 1, 2 => 1, 3 => 1);
if(isset($fids[$fid]))
{
}
else
{
}
Reply
#4
Thanks much Smile

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.
- Jon T. & Kim B.
- AirborneFive Co-Owner's
- Optimized Web Hosting Solutions | Starting @ Only $2/mo
- First Month of Service Free! | Coupon: FIRSTMONTHFREE
Reply
#5
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
$mystuff = array(1, 2, 3);

In some function:
function myfunction()
{
 global $mystuff;
 var_dump($mystuff); //works
}
function myfunction2()
{
 var_dump($GLOBALS['mystuff']); // works too
}
Reply
#6
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.
- Jon T. & Kim B.
- AirborneFive Co-Owner's
- Optimized Web Hosting Solutions | Starting @ Only $2/mo
- First Month of Service Free! | Coupon: FIRSTMONTHFREE
Reply
#7
Two methods:
1) Place the code in a function - ie:
function is_valid_forum()
...
2) Place a global switch - ie
if(...)
 $validForum = true;
else
 $validForum = false;
Reply
#8
ZiNga BuRgA Wrote:I would use a switch - it gives the best performance:
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 Toungue
Eg:
$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 Smile.

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!
- Jon T. & Kim B.
- AirborneFive Co-Owner's
- Optimized Web Hosting Solutions | Starting @ Only $2/mo
- First Month of Service Free! | Coupon: FIRSTMONTHFREE
Reply
#9
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:
	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
	}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)