1.2.362. Do Not Cast To Int¶
Do not cast floats values to int. Uses conversion functions like intval(), round(), floor() or ceil() to convert the value to integer, with known behavior.
Use functions like floor(), round() or ceil() : they use an explicit method for rounding, that helps keeping the side effects under control.
<?php
// echoes 7!
echo (int) ( (0.1 + 0.7) * 10 );
?>
See also Integers.
1.2.362.1. Connex PHP features¶
1.2.362.1.1. Suggestions¶
Upgrade PHP version to 8.0, as those behavior are now the same
Use one of the dedicated function : intval(), round(), floor() or ceil()
1.2.362.1.2. Specs¶
Short name |
Php/NoCastToInt |
Rulesets |
|
Exakat since |
0.10.6 |
PHP Version |
With PHP 8.0 and older |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |