note 129028 added to function.explode

From: Date: Fri, 10 Nov 2023 01:12:35 +0000
Subject: note 129028 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-220984@lists.php.net to get a copy of this message
If your data is smaller than the expected count with the list expansion: <?php $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell,$nu) = explode(":", $data); ?> The result is a warning not an error: PHP Warning: Undefined array key 7 in ... The solution is to pad the array to the expected length: <?php $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell,$nu) = array_pad( explode(":", $data), 8, ""); // where 8 is the count of the list arguments ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 75.178.40.76) ---- Manual Page -- https://php.net/manual/en/function.explode.php Edit -- https://main.php.net/note/edit/129028 Del: integrated -- https://main.php.net/note/delete/129028/integrated Del: useless -- https://main.php.net/note/delete/129028/useless Del: bad code -- https://main.php.net/note/delete/129028/bad+code Del: spam -- https://main.php.net/note/delete/129028/spam Del: non-english -- https://main.php.net/note/delete/129028/non-english Del: in docs -- https://main.php.net/note/delete/129028/in+docs Del: other reasons-- https://main.php.net/note/delete/129028 Reject -- https://main.php.net/note/reject/129028 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220984) next »