jeffjohnvol Posted December 20, 2007 Share Posted December 20, 2007 I know this is a noobish question, but is there an easier way to write: if ($YN=="Y"){ $YSelected="selected"; } elseif ($YN=="N") { $NSelected = "selected"; } I thought there was a way to do this on a single line. Quote Link to comment https://forums.phpfreaks.com/topic/82553-solved-how-to-do-a-conditional-assignment/ Share on other sites More sharing options...
The Little Guy Posted December 20, 2007 Share Posted December 20, 2007 <?php if ($YN=="Y") $YSelected="selected"; else $NSelected = "selected"; ?> Since each if/else has one statement, you don't need the braces... Quote Link to comment https://forums.phpfreaks.com/topic/82553-solved-how-to-do-a-conditional-assignment/#findComment-419682 Share on other sites More sharing options...
revraz Posted December 20, 2007 Share Posted December 20, 2007 If you want to rework your code, you could do $selected = ($YN == "Y") ? "YES" : "NO"; Quote Link to comment https://forums.phpfreaks.com/topic/82553-solved-how-to-do-a-conditional-assignment/#findComment-419691 Share on other sites More sharing options...
jeffjohnvol Posted December 20, 2007 Author Share Posted December 20, 2007 I think you gave me what I needed.... I'll just do: $Yselected = ($YN == "Y") ? "selected" : ""; $Nselected = ($YN == "N") ? "selected" : ""; Thanks, as well as the reminder about the braces. Quote Link to comment https://forums.phpfreaks.com/topic/82553-solved-how-to-do-a-conditional-assignment/#findComment-419695 Share on other sites More sharing options...
corbin Posted December 20, 2007 Share Posted December 20, 2007 You could condense that into: ($YN == 'Y') ? $Yselected = 'SELECTED' : $Nselected = 'SELECTED'; (Assuming $YN is always either Y or N) Quote Link to comment https://forums.phpfreaks.com/topic/82553-solved-how-to-do-a-conditional-assignment/#findComment-419699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.