Learning new array- and string-handling techniques
There have been a number of improvements in PHP 8 array- and string-handling techniques. Although there is insufficient space in this book to cover every single enhancement, we will examine the more significant improvements in this section.
Working with array_splice()
The array_splice() function is a cross between substr() and str_replace(): it lets you replace a subset of one array with another. Its use gets awkward, however, when all you need to do is replace the last part of the array with something different. A quick look at the syntax reveals where it starts to get inconvenient—the replacement parameter is preceded by the length parameter, as illustrated here:
array_splice(&$input,$offset[,$length[,$replacement]]):array
Traditionally, developers first run count() on the original array and use that for the length argument, as shown here:
array_splice($arr, 3, count($arr), $repl);
In PHP 8, the third...