0
public static function select($name, $options_array, $key_as_value=true, $selected="!NO!")
    {
        $html = "<select name='$name'>\n";

        foreach($options_array as $key => $val)
        {
            $option_value = ($key_as_value) ? $key : $val;
            $option_display = $val;
            $selected = ($selected != "!NO!" && $selected == $option_value) ? " SELECTED" : "";

            $option = "<option value='$option_value'$selected>$option_display</option>\n";
            $html .= $option;
        }

        $html .= "</select>\n";

        return $html;
    }

This is my PHP code that generates a SELECT element, if you pass $selected it will have the desired option selected. The problem is, having the default value as "!NO" looks very ugly. But I can't make it as FALSE either because FALSE is synonim for 0 and if there is an option with value "0" it won't work as expected.

Any suggestions for this?

user1091856
  • 3,032
  • 6
  • 31
  • 42
  • 1
    How about null? You use $selected as both parameter and in-loop variable. Are you sure? What happens if one of the val / option_value is " SELECTED" or blank? – Ken Cheung Dec 17 '11 at 04:05
  • `FALSE` is not a synonym for `0`. Yes, `0 == FALSE`, but `0` is an `int` and `FALSE` is a `bool`. Therefore `0 !== FALSE`. See the answer by @dkamins for more information. – Michael Mior Dec 17 '11 at 04:07
  • I try copying your code to run, are you displaying the val or the key? – Ken Cheung Dec 17 '11 at 04:10

1 Answers1

2

You can use FALSE as long as you use the triple equal operator ===. Here are some good links explaining this:

Community
  • 1
  • 1
dkamins
  • 21,450
  • 7
  • 55
  • 59