Reputation: 28188
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?
Upvotes: 0
Views: 3049
Reputation: 99081
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'];
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)Upvotes: 0
Reputation: 4704
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'];
Upvotes: 1