Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
  #1  
Old 05-22-2015, 08:28 AM
Mickie D Mickie D is offline
 
Join Date: Jun 2002
Posts: 430
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Array not working?

why does this array not work?

I am ok getting around PHP, just not 100%

PHP Code:
if ($vbulletin->userinfo['usergroupid'] != array(6,7)) 
thank you very much
Reply With Quote
  #2  
Old 05-22-2015, 08:43 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:

PHP Code:
if (in_array($vbulletin->userinfo['usergroupid'], array(6,7))) 

There's also a vbulletin function called is_member_of() that you can use:
PHP Code:
if (is_member_of($vbulletin->userinfo67)) 

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 05-22-2015, 08:49 AM
Mickie D Mickie D is offline
 
Join Date: Jun 2002
Posts: 430
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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:

PHP Code:
if (in_array($vbulletin->userinfo['usergroupid'], array(6,7))) 

There's also a vbulletin function called is_member_of() that you can use:
PHP Code:
if (is_member_of($vbulletin->userinfo67)) 

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

PHP Code:
if ($vbulletin->userinfo['usergroupid'] != AND 7
Reply With Quote
  #4  
Old 05-22-2015, 08:56 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Mickie D View Post
PHP Code:
if ($vbulletin->userinfo['usergroupid'] != AND 7

That would work for checking 6 but not 7. What you'd want is this:
PHP Code:
if ($vbulletin->userinfo['usergroupid'] != 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().
Reply With Quote
  #5  
Old 05-22-2015, 10:15 AM
Mickie D Mickie D is offline
 
Join Date: Jun 2002
Posts: 430
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
That would work for checking 6 but not 7. What you'd want is this:
PHP Code:
if ($vbulletin->userinfo['usergroupid'] != 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
PHP Code:
 if ($vbulletin->userinfo['usergroupid'] != AND $vbulletin->userinfo['usergroupid'] != 7
but this does not?

PHP Code:
 if ($vbulletin->userinfo['usergroupid'] != AND 7
Thank you KH99
Reply With Quote
  #6  
Old 05-22-2015, 10:20 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

When you use 'AND', then the expression is true only if the expressions on both sides of AND are true. In this code:
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:
PHP Code:
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:
PHP Code:
if (($vbulletin->userinfo['usergroupid'] != 6) AND (7)) 
which is obviously not what you want.
Reply With Quote
  #7  
Old 05-22-2015, 10:54 AM
Mickie D Mickie D is offline
 
Join Date: Jun 2002
Posts: 430
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #8  
Old 05-22-2015, 11:38 AM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
Благодарность от:
Mickie D
  #9  
Old 05-22-2015, 12:18 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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.
Reply With Quote
2 благодарности(ей) от:
MarkFL, Mickie D
  #10  
Old 05-22-2015, 03:03 PM
Mickie D Mickie D is offline
 
Join Date: Jun 2002
Posts: 430
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
Благодарность от:
MarkFL
Reply

Thread Tools
Display Modes

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

Forum Jump


All times are GMT. The time now is 05:15 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04401 seconds
  • Memory Usage 2,283KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (13)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (10)post_thanks_box
  • (4)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (3)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete