View Full Version : Array not working?
Mickie D
05-22-2015, 08:28 AM
why does this array not work?
I am ok getting around PHP, just not 100%
if ($vbulletin->userinfo['usergroupid'] != array(6,7))
thank you very much
That doesn't work because what you're doing is comparing an integer ($vbulletin->userinfo['usergroupid']) to an array, which doesn't do what you're expecting. There's a php function called in_array() (http://php.net/manual/en/function.in-array.php) that does what you want:
if (in_array($vbulletin->userinfo['usergroupid'], array(6,7)))
There's also a vbulletin function called is_member_of() that you can use:
if (is_member_of($vbulletin->userinfo, 6, 7))
that will also check the secondary usergroups (which may or may not be what you want, but unless you have some very complicated usergroup system, it probably does what you want).
Mickie D
05-22-2015, 08:49 AM
That doesn't work because what you're doing is comparing an integer ($vbulletin->userinfo['usergroupid']) to an array, which doesn't do what you're expecting. There's a php function called in_array() (http://php.net/manual/en/function.in-array.php) that does what you want:
if (in_array($vbulletin->userinfo['usergroupid'], array(6,7)))
There's also a vbulletin function called is_member_of() that you can use:
if (is_member_of($vbulletin->userinfo, 6, 7))
that will also check the secondary usergroups (which may or may not be what you want, but unless you have some very complicated usergroup system, it probably does what you want).
cheers Kh99
I actually made it work with AND, well i think it worked ? I have not asked the moderators to test the script yet????
if ($vbulletin->userinfo['usergroupid'] != 6 AND 7)
if ($vbulletin->userinfo['usergroupid'] != 6 AND 7)
That would work for checking 6 but not 7. What you'd want is this:
if ($vbulletin->userinfo['usergroupid'] != 6 AND $vbulletin->userinfo['usergroupid'] != 7)
Oh, and in my post above I forgot that you were checking for !=, so you'd of course want to use !in_array() and !is_memebr_of().
Mickie D
05-22-2015, 10:15 AM
That would work for checking 6 but not 7. What you'd want is this:
if ($vbulletin->userinfo['usergroupid'] != 6 AND $vbulletin->userinfo['usergroupid'] != 7)
Oh, and in my post above I forgot that you were checking for !=, so you'd of course want to use !in_array() and !is_memebr_of().
brilliant KH99, Yes I know that != means does not equal :)
I really wanted to like your post lol, but it wont let me because i liked a post of yours 6 months ago.
Also just out of curiosity why does this work
if ($vbulletin->userinfo['usergroupid'] != 6 AND $vbulletin->userinfo['usergroupid'] != 7)
but this does not?
if ($vbulletin->userinfo['usergroupid'] != 6 AND 7)
Thank you KH99
When you use 'AND', then the expression is true only if the expressions on both sides of AND are true. In this code:
if ($vbulletin->userinfo['usergroupid'] != 6 AND 7)
the left side is $vbulletin->userinfo['usergroupid'] != 6, and the right side is just 7. As it turns out (using php the rules of type conversion), an integer converts to 'false' if it's 0 and true otherwise, so the expression '7' is always true. So the result is the same as this:
if ($vbulletin->userinfo['usergroupid'] != 6)
I hope that makes sense.
Or maybe a better way to put it would be that the != has higher precedence than AND, so if you were to put parens in the expression to make it clear, what you'd have is this:
if (($vbulletin->userinfo['usergroupid'] != 6) AND (7))
which is obviously not what you want.
Mickie D
05-22-2015, 10:54 AM
So the AND does not look at the code as
usergroup 6 and usergroup 7
it looks at it like
usergroup 6 and 7
because the 6 is on the left of the AND, would that also be true with OR
Thanks KH99
Indeed, but if you're going to compare 1 variable with multiple variables, it's best to just use the in_array function to keep the code clean.
So the AND does not look at the code as
usergroup 6 and usergroup 7
it looks at it like
usergroup 6 and 7
because the 6 is on the left of the AND, would that also be true with OR
Well, yes, I think. The expression ($x != 6 AND 7) has 3 values separated by 2 operators, so in what order would you evaluate it? It turns out that != has higher precedence than AND (as you can see in this table of operator precedence (http://php.net/manual/en/language.operators.precedence.php)), so that means the 6 goes with the !=, and the result of that is ANDed with 7.
But I should also point out (just to make it more confusing) that even if AND were higher so that the expression 6 AND 7 were evaluated first, it still doesn't do what you'd want, so in a way I guess this is an overly complicated answer to your question. I suppose it's just that sometimes if you read the code like it's English it means something different than it does in php, so $x != 6 AND 7 might make sense in English, but it's not correct php (or at least it's not correct for what you're trying to do).
Live Dave says, it's usually easier to use in_array(), although I feel that if you only have 2 values then ANDing 2 checks is not so bad. But I'm not an expert php programmer, escpecially when it comes to style.
Oh, and yeah, OR would be the same.
Mickie D
05-22-2015, 03:03 PM
Thank you it does make allot of sense...
I am quite good at building smaller scripts and making things work, but I am trying to build a large-ish plugin for my community but it seems a bit challenging with my knowledge.
However I think to improve my skills I need to have a go :)
Again thanks KH99 I really appreciate the explanation and your time.
Mick
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.