galvin Posted November 3, 2010 Share Posted November 3, 2010 I have a session variable that will be set to a number called... $_SESSION['number'] I have page where I want to set a variable in the format of $serv# (with "#" being the value of the session variable). Is there a way to write one simple line of code that in effect says... $serv . $_SESSION['number'] = "selected='selected'"; I'm just looking to avoid having to write this code... if ($_SESSION['number'] == 1) { $serv1 = "selected='selected'"; } elseif ($_SESSION['number'] == 2) { $serv2 = "selected='selected'"; } etc etc It's basically putting a variable inside of a variable and I'm not sure if this is allowed/proper? Any insight would be appreciated. Thanks, Gary Link to comment https://forums.phpfreaks.com/topic/217614-session-variable-in-variable/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 3, 2010 Share Posted November 3, 2010 $serv1 = $serv2 = ^^^ Don't use a series of numbered variables. Use an array, then the $_SESSION['number'] variable can simply be used as the array index - $serv[$_SESSION['number']] = Link to comment https://forums.phpfreaks.com/topic/217614-session-variable-in-variable/#findComment-1129715 Share on other sites More sharing options...
kenrbnsn Posted November 3, 2010 Share Posted November 3, 2010 You want to use variable variables <?php ${serv . $_SESSION['number']} = "selected='selected'"; ?> But a clear way of doing this would be to use a array: <?php $serv = array(); $serv[$_SESSION['number']] = "selected='selected'"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/217614-session-variable-in-variable/#findComment-1129716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.