mirror of
https://github.com/danog/psalm.git
synced 2024-12-02 09:37:59 +01:00
Improve example code
This commit is contained in:
parent
51f92dd1e5
commit
33dc3a62be
@ -18,13 +18,17 @@ echo $arr["b"];
|
|||||||
|
|
||||||
You can use the null coalesce operator to provide a default value in the event the array offset doesn’t exist:
|
You can use the null coalesce operator to provide a default value in the event the array offset doesn’t exist:
|
||||||
|
|
||||||
```
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
echo $arr["b"] ?? 0;
|
echo $arr["b"] ?? 0;
|
||||||
```
|
```
|
||||||
|
|
||||||
Alternatively, you can ensure that the array offset always exists:
|
Alternatively, you can ensure that the array offset always exists:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
if (rand(0, 1)) {
|
if (rand(0, 1)) {
|
||||||
$arr = ["a" => 1, "b" => 2];
|
$arr = ["a" => 1, "b" => 2];
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user