php - 使用单选按钮返回布尔值 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; ?> />
我似乎无法将返回值变成布尔值?我仍然得到值是和否:(但不是真或假。有人可以帮忙吗?我已经尝试过这个答案,但没有运气。
解决方案
你可以使用这个技巧:
<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 之外的所有其他整数都被视为真布尔值。
推荐阅读
- c# - 将 T-SQL string_agg 转换为 LINQ C#
- kubernetes - kubernetes kubelet 是 DaemonSet 吗?
- javascript - Javscript 的 String.charAt 方法是否具有 O(1) 时间复杂度?
- excel - 忽略下拉列表中带有公式的空白单元格
- php - SonataAdmin 自定义数据网格过滤器模板
- python - 在 Python 中使用 Lambda 过滤数据框
- php - PHP 无法连接到 SQL Server 2016
- reactjs - React Bootstrap 选项卡一次显示所有内容
- c++ - #include 的问题
在 MacOS 上 - excel - 数据透视表过滤器没有数据 VBA