Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - In "===" what is evaluated first? Either type or value equality?

Tags:

php

We have a comparison operator which has a operator '==='. Can someone guide, what is evaluated first, the "type" or the value equality?

like image 892
OM The Eternity Avatar asked Jan 28 '26 15:01

OM The Eternity


2 Answers

Type first

The type check is first. It is not possible to compare variables of different types without first casting them both to the same type.

like image 131
AD7six Avatar answered Jan 30 '26 04:01

AD7six


I think it is the type because ===does equalty without conversion but sometimes values are coded identically like "0" and "false". As 0 !== false I think the first thing tested is type.

According to source code :

ZEND_API int is_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
{
  Z_TYPE_P(result) = IS_BOOL;
  if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
     Z_LVAL_P(result) = 0;
     return SUCCESS;
  }
  /*then value check*/
like image 45
artragis Avatar answered Jan 30 '26 05:01

artragis