website/errors/while.condNotBoolean.md
<?php declare(strict_types = 1);
$someString = 'hello';
while ($someString) {
$someString = '';
}
This error is reported by phpstan-strict-rules.
The condition of the while loop is not a boolean value. PHP will implicitly convert the value to a boolean using its loose truthiness rules, which can lead to unexpected behavior. For example, 0, '', '0', [], and null are all falsy, while other values of the same types are truthy. Relying on implicit type coercion in conditions makes the code harder to reason about.
Use an explicit boolean comparison:
$someString = 'hello';
-while ($someString) {
+while ($someString !== '') {
$someString = '';
}
Or cast to boolean explicitly to make the intent clear:
$someString = 'hello';
-while ($someString) {
+while ((bool) $someString) {
$someString = '';
}