View Full Version : Best way to set a variable within an if statement

04-27-2012, 05:18 AM
Just looking for some suggestions or thoughts here, nothing too crazy!

I'd like to set the variable $flags as one of three possible values, depending on the return value of variable_get() or flag_get_flags()-- the first of which should take precedence. I'd also like to run a few additional functions if that value is not NULL.

Here's the working code:

if ($flags = variable_get('flag_types')) {
elseif ($flags = flag_get_flags('user', NULL, $user)) {
else {
$flags = NULL;

if ($user_flags) {
// Do stuff.

Both variable_get() and flag_get_flags() will return either an object or NULL.

This code is very ugly to me. What's the most elegant way to write this? There must be a better way!

I tried this:

if ($flags = variable_get('flag_types') || $flags = flag_get_flags('user', NULL, $user)) {


It does execute the control statement correctly, but it doesn't set the value of $flags correctly.. If one of the functions returns an object, $flags is just set to 1.

Looking for insight and suggestions, thanks!