1.2.1290. Unsupported Types With Operators¶
Arrays, resources and objects are generally not accepted with unary and binary operators.
The operators are +, -, *, /, **, %, <<, >>, &, |, ^, ~, ++ and –. In PHP 8.0, the rules have been made stricter and more consistent.
The only valid operator is +, combined with arrays in both operands. Other situations throw TypeError
.
<?php
var_dump([] % [42]);
// int(0) in PHP 7.x
// TypeError in PHP 8.0 +
// Also impossible usage : index are string or int
$a = [];
$b = $c[$a];
?>
See also Stricter type checks for arithmetic/bitwise operators and TypeError.
1.2.1290.2. Connex PHP features¶
1.2.1290.2.1. Suggestions¶
Do not use those values with those operators
Use a condition to skip this awkward situation
Add an extra step to turn this value into a valid type
1.2.1290.2.2. Specs¶
Short name |
Structures/UnsupportedTypesWithOperators |
Rulesets |
|
Exakat since |
2.1.9 |
PHP Version |
With PHP 8.0 and older |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Medium |
Available in |