What does “===” mean?

前端 未结 10 1495
一生所求
一生所求 2020-11-27 06:01

I\'ve noticed someone using the PHP operator === which I can\'t make sense out of. I\'ve tried it with a function, and it corresponds in crazy ways.

Wha

10条回答
  •  鱼传尺愫
    2020-11-27 06:44

    In PHP you may compare two values using the == operator or === operator. The difference is this:

    PHP is a dynamic, interpreted language that is not strict on data types. It means that the language itself will try to convert data types, whenever needed.

    echo 4 + "2"; // output is 6
    

    The output is integer value 6, because + is the numerical addition operator in PHP, so if you provide operands with other data types to it, PHP will first convert them to their appropriate type ("2" will be converted to 2) and then perform the operation.

    If you use == as the comparison operator with two operands that might be in different data types, PHP will convert the second operand type, to the first's. So:

    4 == "4" // true

    PHP converts "4" to 4, and then compares the values. In this case, the result will be true.

    If you use === as the comparison operator, PHP will not try to convert any data types. So if the operands' types are different, then they are NOT identical.

    4 === "4" // false

提交回复
热议问题