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:
parent
44713a48ac
commit
dd0898c4df
@ -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 doesn’t 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"];
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user