vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Array not working? (https://vborg.vbsupport.ru/showthread.php?t=318756)

Mickie D 05-22-2015 08:28 AM

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

kh99 05-22-2015 08:43 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() 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).

Mickie D 05-22-2015 08:49 AM

Quote:

Originally Posted by kh99 (Post 2546154)
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


kh99 05-22-2015 08:56 AM

Quote:

Originally Posted by Mickie D (Post 2546155)
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().

Mickie D 05-22-2015 10:15 AM

Quote:

Originally Posted by kh99 (Post 2546157)
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

kh99 05-22-2015 10:20 AM

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.

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

Dave 05-22-2015 11:38 AM

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.

kh99 05-22-2015 12:18 PM

Quote:

Originally Posted by Mickie D (Post 2546171)
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.

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


All times are GMT. The time now is 03:58 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.01197 seconds
  • Memory Usage 1,763KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (13)bbcode_php_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete