On my edit pages, how would I show the correct radio button if for $subscribedrips is equal to Yes or No? Here is what I have and its not working:
if ($row['subscribedrips'] == Yes) {
echo
'<input type="radio" name="subscribedrips" value="Yes" CHECKED /> Yes
<input type="radio" name="subscribedrips" value="No" /> No';
}
elseif ($row['subscribedrips'] == No) {
echo
'<input type="radio" name="subscribedrips" value="Yes" /> Yes
<input type="radio" name="subscribedrips" value="No" CHECKED/> No';
}
elseif (empty($row['subscribedrips'])) {
echo
'<input type="radio" name="subscribedrips" value="Yes" CHECKED/> Yes
<input type="radio" name="subscribedrips" value="No" /> No';
}
Something like this
works. It uses the ternary operator to either insert
'CHECKED'or an empty string into the input tag, based on the value of$subscribedrips.You could also do in a more verbose manner, for example with switch:
Personal preference really.
Updated Snippet 1
Updated Snippet 2
Regarding your last question, the difference between our approaches is pretty simple, but once again (ahh!) its a style choice, both accomplish the same goal, both methods are used in “production” PHP code.
My example builds the entire input tag in PHP and prints it. Valentinas’ approach pulls the static text out of the PHP strings and puts it directly into HTML.
For example, the following lines will all result in the same output:
I’m doubtful there is any significant performance difference between the two methods, but there is one cosmetic differences that I’ll highlight.
Syntax highlighting – If you use an editor with syntax highlighting, valentinas’ approach will allow the syntax highlighter to appropriately highlight the
inputtag and its attributes. Using my approach, the entire string would be highlighted the same. Here is a screenshot showing how notepad++ highlights the two methods.As you can see valentinas’ approach results in a more colorful display, which could help identify and track down errors.
There are some subtle differences when it comes to how your code has to be formatted if you want to conditionally print the entire tag, but they’re not really worth talking about — the biggest, in my opinion, is the syntax highlighting.