A string position or array key that can be 0 is tested for truth

tx0/php:position-as-booleanrecommendedwarningreliability/logicdepth fastbug

What it finds

if (strpos($haystack, $needle)): strpos and its relatives return the position of a match, or false when there is none. A match at the very start is position 0, which is falsy, so a truth test, !, or a loose comparison with false or 0 takes a match at the start for no match. array_search returns the key, which is 0 for the first element of a list, and has the same trap. === false and !== false compare correctly.

From the rule's source, position_as_boolean.rs.

Example from the rule's tests
<?phpfunction find($haystack, $needle, $list){    if (strpos($haystack, $needle)) {tx0 reports: the result of strpos is used as a condition        return 1;    }    if (!stripos($haystack, 'x')) {tx0 reports: the result of stripos is negated with !        return 2;    }    if (array_search($needle, $list) == false) {tx0 reports: the result of array_search is compared loosely with a falsy value        return 3;    }    if (strpos($haystack, $needle) !== false) {        return 4;    }    $at = strpos($haystack, $needle);    return $at === false ? 0 : $at;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/php:position-as-boolean" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/php:position-as-boolean] reason="..."

tx0 explain tx0/php:position-as-boolean shows the same in the terminal. Found a false positive? Report it.