Q: Trying to change statement so create_radio function is only called once (PHP) I have a function that creates a radio button in PHP:
// This function creates a radio button.
// The function takes two arguments: the value and the name.
// The function also makes the button "sticky".
function create_radio($value, $name = 'gallon_price') 
{
    // Start the element:
    echo '<input type="radio" name="' . 
        $name .'" value="' . $value . '"';

    // Check for stickiness:
    if (isset($_POST[$name]) && ($_POST[$name] == $value)) 
    {
        echo ' checked="checked"';
    } 

    // Complete the element:
    echo " /> $value ";
} // End of create_radio() function.

I then leave the PHP form to create an html form and call the function three times with values that represent three different gas prices.
<span class="input">
        <?php
            create_radio('3.00');
            create_radio('3.50');
            create_radio('4.00');
        ?>
    </span>

I am wondering how I could change this code so it would be possible to get the same output and only make one call to the create_radio function.
Thanks!

A: you could make $value an array create_radio(array('3.00','3.50','4.00')); just loop inside the function:
function create_radio($value,$name = 'gallon_price'){

    foreach($value as $v){
        // Start the element:
        $out. = '<input type="radio" name="'.$name.'" value="'.$v.'"';

        // Check for stickiness:
        if(isset($_POST[$name])&&($_POST[$name]==$v)){
            $out .= ' checked="checked"';
        }

        // Complete the element:
        $out .= " /> $v ";
    }
    return $out;

} // End of create_radio() function.

call it:
echo create_radio(array('3.00','3.50','4.00'));

it is usually better not to echo inside the function.

A: function create_radio($value, $name = 'gallon_price') 
{
    $output = "";
    if (is_array($value)) {
        while (count($value) > 0) {
            $arr_value = array_pop($value);
            $output .= create_radio($arr_value);
        }
    } else {
        // Start the element:
        $output .= '<input type="radio" name="' . 
        $name .'" value="' . $value . '"';

        // Check for stickiness:
        if (isset($_POST[$name]) && ($_POST[$name] == $value)) 
        {
           $output .= ' checked="checked"';
        } 

     // Complete the element:
        $output .= " /> $value ";
    }
    return $output;
}

A quick bit of recursion will allow the function to work for arrays and non arrays.  Note: in the html you will need to echo the call to create_radio not just call it.
