【问题标题】:Trouble declaring variables from a PHP function? [closed]从 PHP 函数声明变量时遇到问题? [关闭]
【发布时间】:2015-05-02 16:40:31
【问题描述】:

我正在编写一个用于检查 POST 数据的函数,并查看是否检查了收音机,如果是,请检查正确的输入。这些单选按钮有很多,所以我认为编写一个函数会更容易。

我遇到了麻烦。

当前代码:

PHP:

function foo_check($foo_radio, $foo_1, $foo_2) {

    if ($foo_radio == "1") {

        $foo_1 = "checked";

    } elseif ($foo_radio == "2") {

        $foo_2 = "checked";

    }

}

$radio = $_POST["radio"];

foo_check($radio, $radio_1, $radio_2);

HTML:

<input type="radio" name="radio" value="1" <?php echo $radio_1; ?>>1&nbsp;&nbsp;
<input type="radio" name="radio" value="2" <?php echo $radio_2; ?>>2

【问题讨论】:

  • 你到底卡在哪里了
  • 你哪里麻烦了??你到底想做什么??
  • 具体有什么问题
  • 我认为您使用函数参数的方式错误。请更具体地说明您的问题。
  • 您应该阅读Variable Scope,因为您似乎正在更改函数内部的变量,并希望它们在其他代码部分中保持这种状态。

标签: php function variables radio-button


【解决方案1】:
function foo_check($foo_radio) {

    if ($foo_radio == "1") {
        return "Radio1 Checked";
    } elseif ($foo_radio == "2") {
        return "Radio2 Checked";
    }

}

$radio = $_POST["radio"];
$result = foo_check($radio);
echo $result;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多