简体   繁体   English

PHP字符串比较不匹配时返回true

[英]PHP string comparison returning true when they don't match

Why do the following string comparisons return true? 为什么以下字符串比较返回true?

I am using Laravel framework 4.2, the Form::select compares the option value using this method and it's find matches that shouldn't be. 我使用的是Laravel Framework 4.2,Form :: select使用此方法比较选项值,发现不应该存在的匹配项。

  public function testStringComparison1()
  {
    $value = '8000003E-1408739159';
    $selected = '8000010E-1434656519';
    $b = false;
    if ((string) $value == (string) $selected) {
      $b = true;
    }
    $this->assertFalse($b);  // this fails, $b == true
  }

  public function testStringComparison2()
  {
    $value = '8000008E-1408740949';
    $selected = '8000010E-1434656519';
    $b = false;
    if ((string) $value == (string) $selected) {
      $b = true;
    }
    $this->assertFalse($b);  // this fails, $b == true
  }

If you want to make sure the types match as well, not just the values (weird stuff with type casting), use === . 如果要确保类型也匹配,而不仅仅是值(类型转换很奇怪的东西),请使用===

http://php.net/manual/en/language.operators.comparison.php http://php.net/manual/zh/language.operators.comparison.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM