Table of Contents

PHP RFC: Create "split" as an alias to "explode"

Introduction

PHP has the concept of alias functions, where a function can have more than one name. Examples of aliases are sizeof, which is an alias of count, chop, which is an alias of rtrim, die, which is an alias of exit, and join, which is an alias of implode.

In several languages, the counterpart of join is split, but in PHP, we call it explode (counterpart of implode).

This RFC proposes a new alias to the `explode` function called `split`.

<?php
 
// The following lines would be equivalent
$array = explode(' ', 'String with spaces');
$array = split(' ', 'String with spaces');

Proposal

Several languages use the word split for the function that separates a string into an array using a separator and `join` for its counterpart. PHP functions for those purposes are, respectively, `explode` and `implode`.

That difference from the majority of languages was mitigated with a `join` alias to the `implode` function, so a person coming to PHP from a different language has one less “strange” thing to get used to, but there is no `split` alias to `explode`.

In older versions of PHP, there was a function named `split` which was part of the extension `ereg` and could be used to split a string into an array by a regular expression pattern. The `ext/ereg` was deprecated in PHP 5.3 and removed in PHP 7.0. Since then, the `split` function name has not been used and is available.

This RFC has one simple purpose: creating a new function alias called split that will execute the exact same code as explode. Neither parameters nor any behavior will change with this RFC. It will only make the following possible:

<?php
 
$str = 'one|two|three|four';
 
print_r(split('|', $str));
 
print_r(split('|', $str, 2));

Which would result in the following output:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)

Array
(
    [0] => one
    [1] => two|three|four
)

Why add a new alias?

Some people might question how useful a new alias would be, and that is a valid concern.

Whenever we developers learn a new language, we find similarities and differences from the programming languages we already know. When something is similar, we tend to assimilate it faster and feel more comfortable with the feature in question. Whenever something is very different from what we are used to, it adds some cognitive load and can make the process more challenging.

As it was mentioned previously (and added to the references), multiple other programming languages use `split` as the word for this functionality, so having it in PHP would just make people coming from those languages a little bit more comfortable.

As Paul Dragoonis mentioned:

Making it a smoother transition for JS devs to hop back on the modern PHP train is in all our shared interest in terms of user base, and split() would help this.

This applies to other languages too.

In the references, I am adding a small number of “complaints” in social media where users mention that `split` would be an interesting alias.

Backward Incompatible Changes

None

Proposed PHP Version(s)

8.6

Voting Choices

Create a new split alias for the explode function?
Real name Yes No
Final result: 0 0
This poll has been closed.

Patches and Tests

https://github.com/php/php-src/pull/19490

References