Hi all and thanks for reading. I came accross a function on the internet for creating a <select> box out of an array. I understand all of the code except for a few things:
<?php
function dropdown($name, array $options, $selected=null) {
// Begin the select tag.
$dropdown = "<select name=\"$name\" id=\"$name\">\r\n";
$selected = $selected;
// Loop over all the options.
foreach ($options as $key=>$option) {
// Assign a selected value.
$select = $selected==$key ? " selected" : null;
// Add each option to the dropdown.
$dropdown .= "<option value=\"$key\"$select>$option</option>\r\n";
}
// Close the select tag.
$dropdown .= "</select>\r\n";
// Return the completed dropdown.
return $dropdown;
}
?>
<form>
<?php
$name = "dropDown";
$options = array( "dingo", "wombat", "kangaroo" );
$selected = 2;
echo dropdown($name, $options, $selected);
?>
</form>
1) What is the purpose of typing this: "$selected = $selected;" when the name tag did not need to be rebound?
2) "foreach ($options as $key=>$option) {" What is $key?
3) "$select = $selected==$key ? " selected" : null;" can someone break that down to me in plain english?
Thank you very much for reading,
Anthony