Menu

← Back to ternary.*

Error Identifier: ternary.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.

This error is reported by phpstan/phpstan-strict-rules.

Code example #

<?php declare(strict_types = 1);

function getLabel(?string $name): string
{
	return $name ? $name : 'anonymous';
}

Why is it reported? #

The condition of the ternary operator (? :) is expected to be a boolean value when phpstan/phpstan-strict-rules is enabled. Using non-boolean values like strings, integers, or nullable types in boolean contexts can lead to subtle bugs because of PHP’s loose type coercion rules (e.g., 0, "", and "0" are all falsy).

How to fix it #

Use an explicit boolean expression in the ternary condition.

 <?php declare(strict_types = 1);
 
 function getLabel(?string $name): string
 {
-	return $name ? $name : 'anonymous';
+	return $name !== null ? $name : 'anonymous';
 }

How to ignore this error #

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

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

Rules that report this error #

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

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.