简体   繁体   中英

PHP dropdown menu select concatenation

PHP:

  echo '<option' . if(isset($_SESSION['hoopSelect'])){ echo 'selected="'$_SESSION['hoopSelect'])'"';} . 'value='. $row_list['username'] . '>' . $row_list['username'] . '</option>';}

As there is lots of '', "", <>, )(, []. Im getting flustered, my compiler lacks good error checking (I should probably change but today isn't the day) therefore i'm struggling to google it, but it is an internal error therefore my concatenation , I've been researching for hours. Can anyone spot anything obvious?

For context; if the session called hoopSelect is set it will have the dropdown menu == to hoopSelect. Thanks in advance.

How about making your code readable so you won't get confused?

$selected = (isset($_SESSION['hoopSelect'])) ? ' selected="selected"' : '';
$username = $row_list['username'];
printf('<option value="%s"%s>%s</option>', $username, $selected, $username);

here I used ternary operator as a shorthand for your IF statement. I also used the printf() function to format the output string using the values from the variables $selected and $username .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM