Below is a function with a parameter that (in this example case) can contain either ‘cat’, ‘dog’ or ‘bird’. The function contains of a form and I want to be able to set the right radio button as checked, depending on what option the variable $animal contains.
How could this be achieved in an elegant way?
public function SetAsChecked($animal) {
// $animal = 'cat', 'dog' and 'bird'
$html = "
<form method='post'>
<p>Option</p>
<input type='radio' name='animals' value='1'> Cat<br />
<input type='radio' name='animals' value='2'> Dog<br />
<input type='radio' name='animals' value='3'> Bird<br />
</form>";
return $html;
}
Create an array of options instead of repeating HTML: