PHP-8.3.1
This is a code snippet from a php function used in a software package I installed:
$i = 0;
$extra_small_header_field_values = array();
while ($i < $i_len && $i !== false) {
/* get argument */
$read = trim(substr($read,$i));
$i_len = strlen($read);
$i = strpos($read,' ');
$arg = substr($read,0,$i);
++$i;
The line containing ++$i; results in a warning Increment on type bool has no effect,.
My question is why is $i considered a boolean here? I realize that in PHP a boolean is a special class of integer but how does $i get to be considered a boolean given its evident prior treatment as an integer in the preceding code? How is this issue most correctly addressed?
2
Answers
Here a link of the RFC : https://wiki.php.net/rfc/saner-inc-dec-operators.
Simply change ++$i to $i += 1;
this should work for u
the problem is because of how $i is used in the while loop the condition checks if $i is not equal to false and if it is, the loop stops. But here’s the catch if the loop terminates because $i becomes false the next time around it tries to increment a boolean value and that’s a not good, triggering the warning. To fix this you need to make sure you’re not trying to increment a boolean.