【问题标题】:two strings identical, but not equal when compared两个字符串相同,但比较时不相等
【发布时间】:2023-11-21 19:16:01
【问题描述】:

我有一个我自己无法解决的小问题,我正在比较两个相同的字符串,一个来自数据库,另一个来自文本文件,除了最后一个之外,值始终相等

文本文件中的数组代码

while (!feof($monfichier)) {
    $line = fgets($monfichier);
    $pieces = explode(" ", $line);
    $factureAgent[$nbb][0] = $pieces[0]; //id client
    $factureAgent[$nbb][1] = $pieces[1]; //consommation annuelle
    $factureAgent[$nbb][2] = $pieces[2]; //année
    $factureAgent[$nbb][3] = $pieces[3]; //id agent
    $month = explode(":", $pieces[4]);
    $factureAgent[$nbb][4] = $month[1]; //janvier
    $month = explode(":", $pieces[5]);
    $factureAgent[$nbb][5] = $month[1]; //fevrier
    $month = explode(":", $pieces[6]);
    $factureAgent[$nbb][6] = $month[1]; //mars
    $month = explode(":", $pieces[7]);
    $factureAgent[$nbb][7] = $month[1]; //avril
    $month = explode(":", $pieces[8]);
    $factureAgent[$nbb][8] = $month[1]; //mai
    $month = explode(":", $pieces[9]);
    $factureAgent[$nbb][9] = $month[1]; //juin
    $month = explode(":", $pieces[10]);
    $factureAgent[$nbb][10] = $month[1]; //juillet
    $month = explode(":", $pieces[11]);
    $factureAgent[$nbb][11] = $month[1]; //aout
    $month = explode(":", $pieces[12]);
    $factureAgent[$nbb][12] = $month[1]; //septembre
    $month = explode(":", $pieces[13]);
    $factureAgent[$nbb][13] = $month[1]; //octobre
    $month = explode(":", $pieces[14]);
    $factureAgent[$nbb][14] = $month[1]; //novembre
    $month = explode(":", $pieces[15]);
    $factureAgent[$nbb][15] = $month[1]; //decembre
    $nbb++;
}

代码在这里

echo "<br>".$row[0]."is".$factureAgent[$i][$nb]."<br>";
if ($année==$annéeComp[0]) {
  if ($row[0]!=$factureAgent[$i][$nb]) {
    echo "<br>".$row[0]."is".$factureAgent[$i][$nb]."<br>";
    $ok = false;
  }
  $nb++;
  $nbr++;
}

输出

25is25
50is50
87is87
115is115
200是200
250是250
400是400
550是500
600是600
650是650
800是800
950是950
950就是950

see image

我正在从文本文件中读取的行

1 2000 2019 1 01:25 02:50 03:87 04:115 05:200 06:250 07:400 08:550 09:600 10:650 11:800 12:950
13 2000 2019 1 01:45 02:90 03:150 04:300 05:600 06:800 07:1000 08:1300 09:1450 10:1600 11:1800 12:2000

see image

【问题讨论】:

  • 尝试提供更多代码。还请提供 text 输出和输入文件作为.. 你知道.. 文本,而不是图像。
  • @redreinard 好的,我不想在里面放太多代码,但我会尝试添加更多。
  • @redreinard 我添加了一些代码,如果它有帮助,我不会,但请告诉我。
  • 你怎么知道他们不相等?您看到的输出可能来自 PHP 的第一行。
  • @showdev 我有一个总是显示的输出,另一个只显示在 if 内部,而且我还增加了 $nb,所以数组最终到达了它的末尾,问题通过转换解决了字符串转换为 int,但我仍然想知道字符串出现问题的原因。

标签: php string-comparison


【解决方案1】:

比较不同来源的字符串可能会导致意想不到的结果。

您可以做的一件事是将您的值转换为相同的类型,然后进行比较。 例如:

if (intval('1') === intval('01')) ...

这样你就得到了整数的两侧,它只是一个整数比较。

请注意,如果您转换的值不是数字,则函数将始终返回 0

【讨论】:

    【解决方案2】:

    那可能是您使用== 而不是===。 如果你不想考虑这个案例,我可能会建议使用strcasecmp,如果你想考虑这个案例,我可能会建议使用strcmp

    strcasecmp()

    strcmp()

    注意将这些函数的结果与=== 进行比较,以确保它们相等,例如: if (strcmp('Hello, 'Hello') === 0) ...

    使用===== 时要注意

    (1 == '1') //Equal -- not taking into consideration the type but just the value

    (1 === '1') //Not equal -- taking into consideration the type and the value

    仅供参考,请勿使用!=!==

    【讨论】:

    • 我试过了,但没用,我只是在比较数字,所以不区分大小写
    • 尝试使用 intval($value) 将值转换为 int 然后比较
    • 是的,那确实锻炼了,虽然我仍然不知道为什么它不能用字符串锻炼,但谢谢。你应该用那个评论来回答。
    最近更新 更多