php 为空且 0 与“0”的奇怪行为

发布于 2024-12-22 15:35:12 字数 1115 浏览 0 评论 0原文

我正在观看视频教程,其中讲师正在测试表单提交。

他使用的代码是:

$required_fields = array('menu_name', 'position', 'visible');

foreach ($required_fields as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0) {
        $errors[] = $fieldname;
    } 
}

注意 && 之后的 if 语句 中的条件,其中他有 $_POST[$fieldname] != 0< /code>

由于某种原因,这对我不起作用。

但是,当我给出像这样 $_POST[$fieldname] != "0" 这样的 0 引号时,它就会起作用。


顺便说一句,在 MySQL 中,'visible' 字段是一个布尔值,又名 tinyint(1)

该字段的表单 HTML 如下所示:

Visible:
<input type="radio" name="visible" value="1" <?php if ($sel_subject['visible'] == "1") {echo "checked=\"checked\"";} ?> /> Yes
&nbsp;
<input type="radio" name="visible" value="0" <?php if ($sel_subject['visible'] == "0") {echo "checked=\"checked\"";} ?> /> No

有什么想法吗?预先感谢您的帮助。


更新:

我不确定发生了什么,但由于某种原因,代码现在可以在没有引号的情况下运行。抱歉造成任何混乱。

I'm watching a video tutorial where the instructor is testing a form submission.

The code he is using is:

$required_fields = array('menu_name', 'position', 'visible');

foreach ($required_fields as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0) {
        $errors[] = $fieldname;
    } 
}

Notice the condition in the if statement after the && where he has $_POST[$fieldname] != 0

This does not work for me for some reason.

However, when I give the 0 quotes like this $_POST[$fieldname] != "0" then it works.


BTW, the 'visible' field is a boolean, aka tinyint(1), in MySQL.

Here is what the HTML on the form looks like for this field:

Visible:
<input type="radio" name="visible" value="1" <?php if ($sel_subject['visible'] == "1") {echo "checked=\"checked\"";} ?> /> Yes
 
<input type="radio" name="visible" value="0" <?php if ($sel_subject['visible'] == "0") {echo "checked=\"checked\"";} ?> /> No

Any ideas? Thank you in advance for your help.


UPDATE:

I'm not sure what happened, but for some reason the code is working now without the quotes. Sorry for any confusion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小镇女孩 2024-12-29 15:35:12

在比较之前,请执行以下操作:

$fieldName = (int) $_POST[$fieldname];

希望有帮助

Before comparing, do:

$fieldName = (int) $_POST[$fieldname];

Hope it helps

演多会厌 2024-12-29 15:35:12

我认为您已经在 if () 中定义了条件,以确保所有后参数都有一个值,并且不为空,但它们可以有 0 作为值(对于可见参数)。

然而第二个和第三个条件违反了一些 PHP 内部类型转换规则。
如果你为参数传递一个空字符串值,那么empty()将返回true,所以&&操作员会去检查第二个条件。空字符串与整数 0 的比较再次返回 true,因此 $value != 0 返回 false,因此条件不适用。

这就是为什么如果您为“位置”(或任何其他)的必填字段发送空字符串,上面的代码不会将其视为错误并且验证失败。

由于您希望设置所有值且没有空值,但可见字段的值可能为“0”,因此我建议您将比较更改为:

foreach ($required_fields as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] !== "0")) {
        $errors[] = $fieldname;
    } 
}

记住所有 HTTP 参数(GET、POST,...)均已发送作为 PHP 脚本的字符串值。当使用 == 或 != 运算符将字符串与整数 0 进行比较时,会导致 PHP 将字符串值转换为整数,因此 '' 或 '0' 之间没有区别。但通过使用 === 或 !== 运算符并将 0 作为字符串,将不会转换任何值。所以你可以区分“”和“0”。

I think you have defined the conditions in the if () to make sure that all post params would have a value, and are not empty, but they could have 0 as a value (for the visible param).

Yet the second and third conditions are against some PHP internal type conversion rules.
If you pass an empty string value for a paramter, then empty() would return true, so && operator would go to check the second condition. The comparison of an empty string with integer 0 returns true again, and so $value != 0 returns false, so the condition would not apply.

That is why if you send an empty string for the required field of 'position' (or any other), the code above would not consider it as an error and validation fails.

Since you want all values to be set and none empty, but the visible field could have the value of "0", I suggest you change the comparison to this:

foreach ($required_fields as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] !== "0")) {
        $errors[] = $fieldname;
    } 
}

Remember that all HTTP params (GET,POST, ...) are sent as string values to the PHP script. while comparing a string to integer 0 using == or != operators, would cause PHP to convert string values to integers, so there would be no difference between '', or '0'. But by using the === or !== operators and having the 0 as a string, no value would be converted. So you could differentiate between '' and '0'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文