October 29, 2026 - 16:46
PHP 8.5 Features: Practical Examples You Can Run Image
PHP

PHP 8.5 Features: Practical Examples You Can Run

0 Comments

Cleaning a string often means calling several functions in sequence. Changing part of a URL can mean rebuilding it by hand. PHP 8.5 adds tools for expressing those jobs more clearly, plus a way to warn when you forget to use a function's result. Let's try each one with a small program.

These examples require PHP 8.5. An older executable can reject new syntax while parsing the file; a version check inside that same file cannot rescue it. Confirm the interpreter first. The PHP 8.5 announcement provides the broader release overview.

Read a transformation from left to right

Suppose we want to remove surrounding spaces, convert a string to lowercase and replace its remaining spaces with hyphens. The pipe operator, written |>, puts those steps in order. Each step passes its result to the next function:

PHP
<?php
$slug = '  PHP Runtime Guide  '
    |> trim(...)
    |> strtolower(...)
    |> (fn (string $text): string => str_replace(' ', '-', $text));

echo $slug, PHP_EOL;

The output is php-runtime-guide. The dots in trim(...) are actual syntax, not an instruction to fill in missing code. They supply a function that can be called later; PHP calls this a first-class callable. The final fn creates a short unnamed function that takes the incoming string and supplies the other arguments needed by str_replace. See the functional-operator reference for the operator's rules.

Not every expression benefits from piping. A named intermediate value can be clearer when several later operations need it. This example is also deliberately a text transformation, not a production slug policy: Unicode handling and normalization need separate decisions.

Work with a URI instead of splitting strings

In https://example.com/docs?page=2, HTTPS is the scheme, example.com is the host, /docs is the path and page=2 is the query. Splitting these by hand with explode() can fail on other address forms. A URI is a general way to identify a resource; the new URI extension gives us objects for working with its parts. This class follows the parsing rules in the RFC 3986 standard:

PHP
<?php
$uri = new \Uri\Rfc3986\Uri('https://example.com/docs?page=2');

echo $uri->getHost(), PHP_EOL;
echo $uri->withPath('/guides')->toString(), PHP_EOL;

It prints example.com, followed by https://example.com/guides?page=2. The path changes without rebuilding the query string, and the original value is not modified in place. The methods are listed in the Uri\Rfc3986\Uri reference.

A WHATWG URL model is available too. Choose the model appropriate to the application instead of treating different parsing rules as interchangeable. Parsing a URI successfully is not an authorization decision. An application fetching a user-supplied address still needs its own allowed-scheme and destination policy.

Make important return values visible

Imagine a discount function returning 100 after subtracting 20 from 120. It does not change your original price variable. If you call it but never use the returned 100, the calculation achieves nothing. #[\NoDiscard] is a marker placed above the function to request a warning when its result is ignored. PHP calls these markers attributes; the leading backslash selects PHP's built-in NoDiscard class:

PHP
<?php
#[\NoDiscard]
function applyDiscount(int $price, int $discount): int
{
    return max(0, $price - $discount);
}

$newPrice = applyDiscount(120, 20);
echo $newPrice, PHP_EOL;

This prints 100. Calling only applyDiscount(120, 20); without consuming the result produces a warning. When discarding it is intentional, a (void) cast expresses that choice. The attribute is a useful check, not a substitute for types or tests. PHP documents it among the new language features.

Keep your compatibility promise

A package promising PHP 8.3 compatibility cannot put pipe syntax into code those users must parse. The application runtime, CLI tools, test jobs and deployment target all need to match the intended minimum. Editing a Composer requirement does not upgrade the interpreter on a production host.

Run the three programs as isolated files first. Then pick one small part of the application where a feature improves the code. A focused transformation or URI-building routine is a better trial than rewriting everything at once. Before changing the live runtime, follow the upgrade checklist and test the actual application.

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment