Menu

← Back to booleanAnd.*

Error Identifier: booleanAnd.rightNotBoolean

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

$flag = true;
$string = 'hello';

if ($flag && $string) {
	// ...
}

Why is it reported? #

The right side of the && (boolean AND) expression is not a boolean value. PHP will implicitly cast the non-boolean value to bool before evaluating the expression. This implicit type coercion can lead to unexpected behaviour depending on PHP’s type juggling rules.

This rule is part of phpstan-strict-rules and enforces that only boolean values are used with the && operator, making the code’s intent explicit.

In the example above, $string is of type string, not bool, so using it on the right side of && relies on PHP’s loose type coercion.

How to fix it #

Use an explicit comparison to produce a boolean value:

 <?php declare(strict_types = 1);
 
 $flag = true;
 $string = 'hello';

-if ($flag && $string) {
+if ($flag && $string !== '') {
 	// ...
 }

Or convert the value to boolean with a meaningful comparison:

 <?php declare(strict_types = 1);
 
 $flag = true;
 $string = 'hello';

-if ($flag && $string) {
+if ($flag && strlen($string) > 0) {
 	// ...
 }

How to ignore this error #

You can use the identifier booleanAnd.rightNotBoolean to ignore this error using a comment:

// @phpstan-ignore booleanAnd.rightNotBoolean
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: booleanAnd.rightNotBoolean

Rules that report this error #

  • PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule [1] phpstan/phpstan-strict-rules

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.