Menu

← Back to elseif.*

Error Identifier: elseif.condNotBoolean

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);

function classify(string $value): string
{
    if ($value === 'admin') {
        return 'Administrator';
    } elseif ($value) { // error: Only booleans are allowed in an elseif condition, string given.
        return 'User';
    }

    return 'Guest';
}

This rule is provided by the package phpstan/phpstan-strict-rules.

Why is it reported? #

PHP performs implicit type coercion when evaluating conditions. Values like 0, '', '0', [], and null are considered falsy. This implicit coercion can mask bugs – for example, the string '0' is falsy, which may not be the intended behaviour.

Requiring explicit boolean expressions in elseif conditions makes the code’s intent clearer and avoids subtle errors from truthy/falsy coercion.

How to fix it #

Use an explicit comparison that returns a boolean:

 <?php declare(strict_types = 1);
 
 function classify(string $value): string
 {
     if ($value === 'admin') {
         return 'Administrator';
-    } elseif ($value) {
+    } elseif ($value !== '') {
         return 'User';
     }

     return 'Guest';
 }

How to ignore this error #

You can use the identifier elseif.condNotBoolean to ignore this error using a comment:

// @phpstan-ignore elseif.condNotBoolean
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: elseif.condNotBoolean

Rules that report this error #

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

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.