0

I would like to check if the construction of PHP multiple conditions are correct.

if ($role == 'admin') :
    $active_admin = 'active';
elseif ($role == 'mods') :
    $active_mods = 'active';
elseif ($role == 'writer') :
    $active_writer = 'active';
endif;

However, i noticed that there are undefined variables inside my class attribute. What i wanted to know was, "Do i need to create additional conditions to avoid getting Undefined Variable?"

Because in this statement, admin was initialize as active role, then the rest was idle. I get this notification after viewing it in my source.

<br />
<b>Notice</b>:  Undefined variable: active_admin in...

Thanks for the suggestions or comments. Appreciate it.

iMarkDesigns
  • 1,200
  • 2
  • 14
  • 32

1 Answers1

1

You can prefix the command with @ to suppress that message. Though notices should be turned off in a production environment. Initializing your variables would also eliminate the notice, however I believe you are getting the notice at a later line.

Example:

$active_admin = '';
$active_writer = '';
$active_mods = '';

etc.

This achieves the same as your code:

$role = 'admin';
${"active_$role"} = 'active';
echo $active_admin;
kwollaston
  • 54
  • 5