+
is an arithmetic operator in PHP (basically, there's an exception when both operands are arrays, see below). That means, it will implicitly cast operands to numeric type if they are not numeric. Thus, your string will be cast to numeric, getting 5
in the result.
Getting deeper, you may check the implementation of +
:
if (!converted) {
zendi_convert_scalar_to_number(op1, op1_copy, result);
zendi_convert_scalar_to_number(op2, op2_copy, result);
converted = 1;
} else {
zend_error(E_ERROR, "Unsupported operand types");
return FAILURE; /* unknown datatype */
}
This part of code stands for case. When operands are not numeric data type, they will be converted to numerics.
Side note: +
is also available for arrays (where it will mean hash-table merging):
case TYPE_PAIR(IS_ARRAY, IS_ARRAY): {
zval *tmp;
if ((result == op1) && (result == op2)) {
/* $a += $a */
return SUCCESS;
}
if (result != op1) {
*result = *op1;
zval_copy_ctor(result);
}
zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), (void (*)(void *pData)) zval_add_ref, (void *) &tmp, sizeof(zval *), 0);
return SUCCESS;
}