返回 select 元素的 php 函数
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;
}
这是我的 PHP 代码,它生成一个 SELECT 元素,如果您传递 $selected ,它将选择所需的选项。问题是,将默认值设置为“!NO”看起来非常难看。但我也不能将其设置为 FALSE,因为 FALSE 是 0 的同义词,如果有一个值为“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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要使用三等运算符
===
,就可以使用FALSE
。以下是一些很好的链接,解释了这一点:PHP 相等(== 双等)和恒等(=== 三等)比较运算符有何不同?
http://php.net/manual/en/language.operators.comparison.php
You can use
FALSE
as long as you use the triple equal operator===
. Here are some good links explaining this:How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
http://php.net/manual/en/language.operators.comparison.php