0

My PHP is rusty and I'm struggling to figure this out. I'm working with forum software (SMF) and can't really tell the value of the variable I'm trying to check.

All members are assigned a post_group e.g Senior Member

Some users are assigned a special group e.g Moderator.

I want to set the variable $memberType to the group if there is one, if not the post_group.

E.g

User is Moderator and Senior Member. $memberType = 'Moderator'

User is just Junior Member. $memberType = 'Junior Member'

Here's what I've tried:

$memberType = is_null($message['member']['group']) ? $message['member']['group'] : $message['member']['post_group'];

I also tried empty() and isset() instead of is_null but none of them seem to get me a consistent result.

is_null only applies the post_group, empty() only applies the post_group but weirdly not for my "newbie" class. isset() only applies the post.

Any way I can get a consistent result?

2
  • What you're doing is assigning $message['member']['group'] to $message['member']['group'] if $message['member']['group'] is null, it doesn't make any sense to me. are you sure you don't want !is_null? Commented May 2, 2017 at 22:23
  • Your logic is backwards at the moment. You pretty much say: "Is the group null? YES, then assign the null value to $memberType, if there is a group use the post_group". So what you probably want is something like this: $memberType = isset($message['member']['group']) ? $message['member']['group'] : $message['member']['post_group'];. Now the code says "Is a group set? YES, then assign the group, if there is no group set use the post_group" Commented May 2, 2017 at 22:23

2 Answers 2

1

I guess you missed a ! in your question. empty should work in your scenario. Acc to the man page of empty

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

So to sum up something like this should work.

$memberType = empty($message['member']['group']) === FALSE ? $message['member']['group'] : $message['member']['post_group'];
Sign up to request clarification or add additional context in comments.

Comments

0

What you probably need is :

$memberType = empty($message['member']['group']) ? $message['member']['post_group'] : $message['member']['group'];

Basically is says (pseudocode):

IF $message['member']['group'] IS EMPTY :

$memberType = $message['member']['post_group'];

ELSE :

$memberType = $message['member']['group'];

empty():

The following value are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.