note 129605 added to function.list
| From: | pedro@ndiv.com | Date: | Thu, 20 Jun 2024 08:34:51 +0000 |
| Subject: | note 129605 added to function.list | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221391@lists.php.net to get a copy of this message | ||
As simple way to prevent the "Warning: Undefined array key" in PHP8+ or "Notice:
Undefined offset" in PHP7- is to use the union operator with a fixed size array with default
values:
<?php
$string = 'a,b';
list($a, $b, $c) = explode(',', $string) + [null, null, null];
print_r([$a, $b, $c]);
?>
Will output:
----------------
Array
(
[0] => a
[1] => b
[2] =>
)
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2001:818:e253:8400:28d8:1792:fb9d:daf7)
----
Manual Page -- https://php.net/manual/en/function.list.php
Edit -- https://main.php.net/note/edit/129605
Del: integrated -- https://main.php.net/note/delete/129605/integrated
Del: useless -- https://main.php.net/note/delete/129605/useless
Del: bad code -- https://main.php.net/note/delete/129605/bad+code
Del: spam -- https://main.php.net/note/delete/129605/spam
Del: non-english -- https://main.php.net/note/delete/129605/non-english
Del: in docs -- https://main.php.net/note/delete/129605/in+docs
Del: other reasons-- https://main.php.net/note/delete/129605
Reject -- https://main.php.net/note/reject/129605
Search -- https://main.php.net/manage/user-notes.php