1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Add how to fix for PossiblyUndefinedArrayOffset

This commit is contained in:
Matthew Brown 2020-03-20 15:20:54 -04:00 committed by GitHub
parent 44713a48ac
commit dd0898c4df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,5 +10,24 @@ if (rand(0, 1)) {
}
echo $arr["b"];
```
## How to fix
You can use the null coalesce operator to provide a default value in the event the array offset doesnt exist:
```
echo $arr["b"] ?? 0;
```
Alternatively, you can ensure that the array offset always exists:
```php
if (rand(0, 1)) {
$arr = ["a" => 1, "b" => 2];
} else {
$arr = ["a" => 3, "b" => 0];
}
echo $arr["b"];
```