gpt4 book ai didi

PHP - 字符串和整数

转载 作者:行者123 更新时间:2023-12-04 05:14:32 25 4
gpt4 key购买 nike

我的站点使用 PHP 来检查用户使用“get”方法提交的值是否为某个整数。代码类似于

if ($_GET['input']==2) { ... }

但是我最近发现如果用户输入类似 2a 的内容, 2two甚至 2omdodonsos ,它仍然被认为是值 2通过 PHP。但是如果用户输入 23eee , 23twenty23ofnofnonf ,它不被视为 2 .为什么会发生这种情况?将使用此代码:
 if ($_GET['input']="2") { ... }

解决问题?

最佳答案

您可以(并且应该)使用 input filtering清除不良输入:

if (is_null($input = filter_input(INPUT_GET, 'input', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE))) {
// an entry that was not an integer
} elseif ($input === 2) {
// input was 2
}

另见: filter_input()

关于PHP - 字符串和整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14458951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com