Register Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools
  #1  
Old 22 May 2015, 11:28
Mickie D Mickie D is offline
 
Join Date: Jun 2002
Array not working?

why does this array not work?

I am ok getting around PHP, just not 100%


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

thank you very much
__________________
Digitalworldz Satellite Cable TV Forums
Cable, Satellite, Consoles, Mobile Phones, Audio Visual, and general chat
Reply With Quote
  #2  
Old 22 May 2015, 11:43
kh99 kh99 is offline
 
Join Date: Aug 2009
Real name: Kevin
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() that does what you want:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


There's also a vbulletin function called is_member_of() that you can use:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


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).
Reply With Quote
  #3  
Old 22 May 2015, 11:49
Mickie D Mickie D is offline
 
Join Date: Jun 2002
Originally Posted by kh99 View Post
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() that does what you want:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


There's also a vbulletin function called is_member_of() that you can use:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


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????


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

__________________
Digitalworldz Satellite Cable TV Forums
Cable, Satellite, Consoles, Mobile Phones, Audio Visual, and general chat
Reply With Quote
  #4  
Old 22 May 2015, 11:56
kh99 kh99 is offline
 
Join Date: Aug 2009
Real name: Kevin
Originally Posted by Mickie D View Post
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

That would work for checking 6 but not 7. What you'd want is this:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


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().
Reply With Quote
  #5  
Old 22 May 2015, 13:15
Mickie D Mickie D is offline
 
Join Date: Jun 2002
Originally Posted by kh99 View Post
That would work for checking 6 but not 7. What you'd want is this:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


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

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

but this does not?


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Thank you KH99
__________________
Digitalworldz Satellite Cable TV Forums
Cable, Satellite, Consoles, Mobile Phones, Audio Visual, and general chat
Reply With Quote
  #6  
Old 22 May 2015, 13:20
kh99 kh99 is offline
 
Join Date: Aug 2009
Real name: Kevin
When you use 'AND', then the expression is true only if the expressions on both sides of AND are true. In this code:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

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:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.


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:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

which is obviously not what you want.
Reply With Quote
  #7  
Old 22 May 2015, 13:54
Mickie D Mickie D is offline
 
Join Date: Jun 2002
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
__________________
Digitalworldz Satellite Cable TV Forums
Cable, Satellite, Consoles, Mobile Phones, Audio Visual, and general chat
Reply With Quote
  #8  
Old 22 May 2015, 14:38
Dave Dave is offline
 
Join Date: Jun 2010
Real name: Dave
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.
__________________
https://technidev.com - security, development, exploits, vBulletin
dave[at]technidev[dot]com

Contact me for custom vBulletin 3/4 work & server/website management.
Reply With Quote
  #9  
Old 22 May 2015, 15:18
kh99 kh99 is offline
 
Join Date: Aug 2009
Real name: Kevin
Originally Posted by Mickie D View Post
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), 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.

Last edited by kh99; 22 May 2015 at 15:32.
Reply With Quote
  #10  
Old 22 May 2015, 18:03
Mickie D Mickie D is offline
 
Join Date: Jun 2002
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
__________________
Digitalworldz Satellite Cable TV Forums
Cable, Satellite, Consoles, Mobile Phones, Audio Visual, and general chat
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 11:22.

Layout Options | Width: Wide Color: