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. 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... 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"; 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. 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) 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
Archived
This topic is now archived and is closed to further replies.