Skip to content

Developer-Friendly Upgrade In PHP 8.5

This version will make developer life more easier.

Lot of features are there but these are my favorite.

New features in PHP 8.5

  • Pipe operator (|>)
  • New array_first and array_last functions

Pipe operator (|>)

The pipe operator (|>) to chain multiple callables form left to right taken the return value of the left callable and passing it to the right.

For example you have a string to perform some activites. Like make trim spaces, string upper, & shuffle the string.

Old way

php
# Noraml
$string = " CodingwithRK ";
$string = trim($string);
$string = strtoupper($string);
$string = str_shuffle($string);

# Nested
$string = trim(str_shuffle(strtoupper(" CodingwithRK ")));

New way

php
# Noraml
$string = " CodingwithRK "
    |> trim(...)
    |> strtoupper(...)
    |> str_shuffle(...);

# Nested
$string = strtoupper(" CodingwithRK ") |> str_shuffle(...) |> trim(...);

TIP

No temporary varibales, everthing flows left to right. Makes your code much cleaner.

New array_first and array_last functions

We all know hoe annoying it was to get the first or last element of an array. PHP already has reset() and end(), but they move the inernal pointer.

Now we have array_first() and array_last().

php
$languages = ["php", "javascript", "python", "go"];

$first_lang = array_first($languages); // php
$last_lang = array_last($languages); // go

TIP

Simple, intuitive and doesn't mess with array pointer.