Why is === faster than == in PHP?

前端 未结 12 930
有刺的猬
有刺的猬 2020-12-12 10:39

Why is === faster than == in PHP?

12条回答
  •  心在旅途
    2020-12-12 11:18

    In php (c code) value is a "class" like:

    class value
    {
        $int_;
        $float_;
        $string_;
        $array_;
        $object_;
    }
    

    When your are comparing $a == $b and $a is int type, there will be something like:

    if ($a->int_ == $b->int_ || $a->int_ == (int) $b->float_ || $a->int_ == (int) $b->string_ || ...)
    

    but string '1' will not be cast to ascii code 49, it will be 1.

    When you are comparing $a === $b and $a is int type, there will be someting like:

    if ($a->int_ == $b->int_)
    

提交回复
热议问题