【问题标题】:PHP differentating '' and 0 [duplicate]PHP区分''和0 [重复]
【发布时间】:2021-09-16 15:26:13
【问题描述】:

我正在做一个教师成绩簿项目,我遇到了一个奇怪的 PHP 怪癖。我如何区分''和0?我正在尝试使用运算符,但没有成功。以下是一些背景信息:

$array['grades']['brian']['project'] 将为空,因为没有为此作业输入成绩。学生的作业没有得分为零 (0),只是没有输入成绩。该数组值是使用 HTML <input value=''>

定义的

或者,$array['grades']['samuel']['homework'] 的评分为零 (0)。有一个成绩进入,它只是一个不及格的成绩。该数组值是使用 HTML <input value='0'>

定义的

我需要确定未输入成绩和输入零成绩之间的差异,以便我可以适当地更新 SQL 表。我尝试使用运算符if ($array['grades']['samuel']['homework'] === 0),但这不会触发任何事情。

我怎样才能完成这项任务?

示例数组:

Array
(
    [ucid] => 4
    [grades] => Array
        (
            [brian] => Array
                (
                    [project] => 
                    [homework] => 12
                )

            [samuel] => Array
                (
                    [project] => 9
                    [homework] => 0
                )

        )

)

【问题讨论】:

  • 使用=== 运算符。
  • $array['grades']['samuel']['homework']的数据类型是什么,使用===操作符时要确保是整数,而不是字符串
  • is_numericis_your_friend.
  • @catcon 创建 Samuel 作业的 HTML 是 <input value='0'> 而创建 brian 作业的 HTML 是 <input value=''>
  • 不要使用print_r() 可视化数据,而是使用更多信息工具,例如var_export() 或更好的var_dump()

标签: php arrays


【解决方案1】:

您在正确的轨道上 - 只要您正在测试的值是实际的整数,您的 === 运算符应该可以工作。但是,如果它们是 int 的字符串表示形式,则表达式将返回 false。

<?php

$vals = [
    'zero' => 0,
    'zero_string' => '0',
    'empty_string' => '',
    'null' => null,
    'false' => false
];

// If the type of the var is the same as the type of the test, everything's shiny
assert($vals['zero'] === 0, 'Value should be identical to 0');
assert($vals['empty_string'] === '', 'Value should be identical to ""');
assert($vals['null'] === null, 'Value should be identical to null');
assert($vals['false'] === false, 'Value should be identical to false');

// You can force this with type juggling
assert(intval($vals['zero_string']) === 0, 'Value should be identical to 0');
assert((int) $vals['zero_string'] === 0, 'Value should be identical to 0');

// Uh-oh - string zero and int zero are not the same, so this will fail
assert($vals['zero_string'] === 0, 'Value should be identical to 0');

编辑: 这是一种标准化输入的简单方法,这样您就可以只在一个地方担心类型转换。

<?php

function normalizeGrade($input)
{
    if(is_numeric($input))
    {
        return floatval($input);
    }

    return $input;
}

assert(normalizeGrade('0') == 0, 'String zero should be normalized');
assert(normalizeGrade('90.5') == 90.5, 'String float should be normalized to float');
assert(normalizeGrade('') == '', 'Empty string should be unchanged');
assert(normalizeGrade(null) == null, 'Null should be unchanged');

【讨论】:

  • $array['grades']['brian']['project'] 将被设置为 '',因为在 HTML 的输入中看起来像这样:&lt;input value=''&gt; .... 这会有什么不同吗?
  • OK - 我解释了如何在 PHP 中区分零、空字符串和其他 false-y 值,这就是问题所在。这真的是一个“为我编写代码”的问题吗?
  • 不抱歉,我只是不明白,谢谢您的帮助
  • 你最好的选择可能是拥有一个函数来规范化你的所有值,这样你就可以轻松地测试为零,而不必担心到处都是类型转换。查看更新的示例。
  • 请关闭重复的问题而不是回答它们。 Stack Overflow 上有数百万页。所有基本问题在 2021 年(以及更早的几年)都是重复的。通过回答重复项,您可能会阻止 Roomba 适当地从该站点删除冗余内容。
猜你喜欢
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多