1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 10:57:08 +01:00
psalm/docs/running_psalm/issues/PossiblyFalseOperand.md

40 lines
747 B
Markdown
Raw Normal View History

2020-03-19 17:32:49 +01:00
# PossiblyFalseOperand
Emitted when using a possibly `false` value as part of an operation (e.g. `+`, `.`, `^` etc).
```php
2020-03-21 00:13:46 +01:00
<?php
2020-03-21 14:36:21 +01:00
function echoCommaPosition(string $str) : void {
2020-03-21 14:48:35 +01:00
echo 'The comma is located at ' . strpos($str, ',');
2020-03-21 14:36:21 +01:00
}
```
## How to fix
You can detect the `false` value with some extra logic:
```php
<?php
function echoCommaPosition(string $str) : void {
$pos = strpos($str, ',');
if ($pos === false) {
echo 'There is no comma in the string';
}
echo 'The comma is located at ' . $pos;
}
```
Alternatively you can just use a ternary to suppress this issue:
```php
<?php
function echoCommaPosition(string $str) : void {
echo 'The comma is located at ' . (strpos($str, ',') ?: '');
2020-03-19 17:32:49 +01:00
}
```