简体   繁体   中英

Strange behaviour on if into a for

I have this code :

for($nrt=0; $nrt<$actualline; $nrt++) {
    echo $sidesIndexes[$nrt]." - ".$nrt."<br/>";
    if($sidesIndexes[$nrt]==$nrt) {
        echo "am I in??? ".$sidesIndexes[$nrt]." is different than ".$nrt."<br/>";
    }
}

that print :

# - 0
am I in??? # is different than 0
# - 1
# - 2
# - 3
# - 4
# - 5
# - 6
# - 7
# - 8
# - 9
# - 10
# - 11
# - 12
# - 13
# - 14
# - 15
# - 16
# - 17
# - 18
# - 19

am I tired? How is possible to get that am I in??? # is different than 0 am I in??? # is different than 0 message?

When doing the comparison, the "#" string is converted to a numeric value , which gives 0 .

Then yes, 0 == 0.

You should use === to compare with checking the variable types :

if($sidesIndexes[$nrt] === $nrt) {

This will only be true if the two variable are both the same type (like integers, strings, etc...).

I am assuming this is PHP since it looks like it. Zero evaluates to a number of things in PHP. In this case it looks like 0 == null. To have a strong equality comparison where the types are also checked, use ===.

0 == null // true
0 == false // true
0 == 0 // true

0 === null // false
0 === false // false
0 === 0 // true

In PHP (and other loose-typed languages such as Javascript), there is a major issue when comparing two variables of different type using the double-equal operator == .

The two variables are different types, but PHP can only compare entities that are the same type, so when it sees the attempt to compare, it automatically converts the entities so that they are of the same type.

The result of this is that if you compare a string value "#" with numeric zero, it will convert the string into a numeric prior to doing the comparison. Since this results in a numeric of zero value, it equals the numeric zero it is comparing with, resulting in the if() condition returning true.

In some cases, this is actually the desired effect -- for instance, all data that comes into PHP from the browser in $_POST etc will be strings, even though it may actually be numerical data.

However in many cases, the implicit type conversion of double-equal is a major problem.

The solution to this is the tripple-equal operator - === .

This works exactly like the double-equal operator, except that it doesn't perform a type conversion first. If the types are not the same, then it always returns false.

Therefore, if you have a tripple-equal, a string containing the digit "2" would not equal an integer value of 2, and neither of them would equal a floating point value of 2.00. But with double-equal, they would all be equal.

In your case, you need to use the tripple-equal operator. Both have their uses, and you should use whichever one is appropriate to any given situation.

0 is the same as false in PHP. The character '#' is not true in PHP. So, you're asking if false == false, which it does.

If you try a type depending compare then you should use the === operator if($sidesIndexes[$nrt] === $nrt) { .... }

plural of Index is Indices side? do you mean site?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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