25

Coming across some deprecated errors with 8.1 I want to address.

PHP Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated in...

//explode uids into an array
$comp_uids = explode(',', $result['comp_uids']);

$result['comp_uids'] in this case is empty which is why the null error shows. I'm not sure why they are deprecating this ability, but what is the recommended change to avoid this? I'm seeing similar with strlen(): Passing null to parameter #1 ($string) of type string is deprecated and a few others using 8.1.

user756659
  • 3,372
  • 13
  • 55
  • 110
  • 1
    You shouldn't get that if it's an empty string. The message says it's `null`, not `''`. – Barmar Feb 13 '22 at 04:37
  • `$result['comp_uids']` is coming from a db so in this case it is null. – user756659 Feb 13 '22 at 05:48
  • You shouldn't put comma-separated lists in the DB in the first place. https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Barmar Feb 13 '22 at 17:38
  • I'm not... GROUP_CONCAT() - https://mariadb.com/kb/en/group_concat/ – user756659 Feb 14 '22 at 03:31
  • 1
    In that case you could use `IFNULL(GROUP_CONCAT(...), '')` in the SQL query. – Barmar Feb 14 '22 at 05:02
  • I actually did that last night. What was happening was a query result was being returned even if there were no matching results... I assume because of the group_concat that can return null. So along with the ifnull added I am also checking if a specific value (account id in this case) in my result is !empty() rather that just results exist. Definitely an oversight on my part. Didn't realize a result was being returned when nothing matched in this case. – user756659 Feb 14 '22 at 21:43
  • I was just dealing with this and~ if someone really want a quick "fix" for things to continue working you can use error_reporting(E_ALL ^ E_DEPRECATED); It will supress deprecation messages and you can enjoy your work. HOWEVER! Incase in the future, PHP 9 or whatever's design-decicition are to change this to a fatal-error - until then you MUST have fixed your code issues. – Steini Jan 03 '23 at 20:39

4 Answers4

40

Use the null coalescing operator to default the value to an empty string if the value is null.

$comp_uids = explode(',', $result['comp_uids'] ?? '');
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 7
    This is a really frustrating design flaw. If it expects a string, it should implicitly cast it. Now I have thousands of occurrences across my code where I have to put a `(string)` in front of parameters, which just **bloats the code** for no reason. – Chuck Sep 04 '22 at 11:35
  • 2
    The PHP maintainers apparently think that was a design flaw, since that's what it currently does and they're deprecating it. – Barmar Sep 04 '22 at 14:15
13

Just cast the parameter as string, will convert null to ''.

$comp_uids = explode(',', (string)$result['comp_uids']);
Matthew Ho
  • 139
  • 2
5

There's no need to explode null, you know beforehand that it won't return matches. If you really want [''] as result, it's more intuitive to make it explicit:

$comp_uids = $result['comp_uids'] !== null
    ? explode(',', $result['comp_uids'])
    : [''];

I still find this a bit counter-intuitive for your fellow programmers. IMHO, the no UIDs were found concept is better represented by an empty array and, if you can expect empty strings as well, they may well be handled together with null:

$comp_uids = $result['comp_uids'] != ''
    ? explode(',', $result['comp_uids'])
    : [];
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 99% of the time it won't be null, but there is a case where it can be (brand new user in my case) which is why I need to handle it in the logic of the code. – user756659 Feb 14 '22 at 03:34
  • This solution should be more acceptable. If you put an empty string ('') in second parameter of explode() you will get an array [0 => ''] which you probably don't want. I have seen a case where there is a foreach for the array returned from explode() doing some database deletions. Make sure you check if the variable you need to put in the second parameter of explode() is not null and only in this case call explode(). – Ales Rebec Jan 12 '23 at 13:22
0

This works for me:

$comp_uids = explode(',', ''.$result['comp_uids']);

In fact ''.null is ''

FlorinP
  • 1
  • 1