首页 > 解决方案 > 使用单选按钮返回布尔值 true 还是 false?

问题描述

我有以下代码:

<input type="radio" name="mobileActivation" value="no" id="donotactivatemobile" onchange="disable_link();" onclick="checkBoxDisabling()" <?php if (isset($_POST['mobileActivation']) && $_POST['mobileActivation']=="no") $_POST['uclActivation'] = FALSE; ?> />

<input type="radio" name="mobileActivation" value="yes" id="activatemobile" onchange="enable_link();" onclick="checkBoxEnabling()"  <?php if (isset($_POST['mobileActivation']) && $_POST['mobileActivation']=="yes") $_POST['mobileActivation'] = TRUE;  ?> />

我似乎无法将返回值变成布尔值?我仍然得到值是和否:(但不是真或假。有人可以帮忙吗?我已经尝试过这个答案,但没有运气。

标签: php

解决方案


你可以使用这个技巧:

<input type="radio" name="mobileActivation" value="0" /> false <br>
<input type="radio" name="mobileActivation" value="1">   true  <br>

然后您可以检查以下值:

if(isset($_GET['mobileActivation'])) {
    $phone = (bool) $_GET['mobileActivation'];
    if($phone === true)
        echo("mobileActivation is true");
    else {
        echo("mobileActivation is false");
    }
}

这是因为 PHP 中的 0 被视为假布尔值,除 0 之外的所有其他整数都被视为真布尔值。


推荐阅读