有没有办法阻止PHP评估两个字符串“1”和“1.0”是否相同? [关闭]

In PHP, is there a way to stop it evaluating two strings "1" and "1.0" as the same? The code below illustrates what I'm talking about:

<?php
    $str1 = "1";
    $str2 = "1.0";

    if($str1==$str2){
      echo "equal";
    }else{
      echo "not equal";
    }
?>

The problem for me is that I'm trying to check whether a user has changed a value when they submit it back to the server. At the moment if a user changes the value from "1" to "1.0" it should pick up the fact that they've changed the record and allow them to save it to the database - however because PHP evaluates them both as numbers when comparing them it thinks the user has made no changes and doesn't allow them to save it.

Is there any way around this?

You can always use

===

operator.

Inorder to avoid the issue use

=== 

instead of

==

If you want to strictly compare strings, use strcmp():

if (strcmp($str1, $str2) == 0) {
    // they're the same
}

Granted, this is more verbose, but the alternative of using === has been well discussed already.

From the PHP docs:

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. [...]. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

Therefore the == operator makes a number conversion that leads 1 to be equal to 1.0. If you use the === operator instead, each character of the string it compared to the other corresponding character of the string making a char-by-char comparison leading "1" and "1.0" to be two different strings, hence evaluating to false.