PHP 8.5 in Action: Practical Examples



PHP 8.5, slated for release on November 20, 2025, introduces features that make coding more efficient and intuitive. In this post, we explore practical examples of its standout additions, including the pipe operator, new array functions, enhanced error handling, and internationalization tools. Drawing from updates on PHP.Watch, these examples show how PHP 8.5 can simplify your development workflow. Let’s get started!

1. Streamlined Function Chaining with the Pipe Operator (|>)

The pipe operator (|>) simplifies function composition by passing the result of one function directly to the next. This is perfect for data transformations, as shown in this example where we format a user input string:


<?php
$input = "  php 8.5 rocks  ";
$formatted = $input
    |> 'trim'
    |> fn($s) => ucwords($s)
    |> fn($s) => $s . '!!';
echo $formatted; // Outputs: Php 8.5 Rocks!!
?>
            

This approach avoids nested calls like ucwords(trim($input)) . '!!', making the code more intuitive and maintainable.

2. Easy Array Access with array_first and array_last

The new array_first and array_last functions provide direct access to the first and last elements of an array, eliminating the need for workarounds. Here’s an example using a list of fruits and a settings array:


<?php
$fruits = ['mango', 'apple', 'banana', 'orange'];
$settings = ['theme' => 'dark', 'language' => 'en', 'timezone' => 'UTC'];

echo array_first($fruits); // Outputs: mango
echo array_last($fruits); // Outputs: orange
echo array_first($settings); // Outputs: dark
echo array_last($settings); // Outputs: UTC
echo array_first([]); // Outputs: null
?>
            

These functions make array operations cleaner than using reset() or end(), especially in modern PHP applications.

3. Inspecting Error Handling with get_error_handler and get_exception_handler

PHP 8.5’s get_error_handler and get_exception_handler functions let you check the active error and exception handlers, which is great for debugging. Here’s a practical use case:


<?php
set_error_handler(function($errno, $errstr) {
    error_log("Error logged: $errstr");
});

set_exception_handler(function($e) {
    error_log("Exception logged: " . $e->getMessage());
});

var_dump(is_callable(get_error_handler())); // Outputs: bool(true)
var_dump(is_callable(get_exception_handler())); // Outputs: bool(true)

trigger_error("Sample error", E_USER_WARNING); // Logs: Error logged: Sample error
throw new Exception("Sample exception"); // Logs: Exception logged: Sample exception
?>
            

These functions help you verify handler configurations dynamically, streamlining debugging in complex systems.

4. Formatting Lists with IntlListFormatter

The IntlListFormatter class formats lists according to locale rules, ensuring natural output for global users. Here’s an example with a list of programming languages:


<?php
$languages = ['PHP', 'Python', 'JavaScript'];

$formatterEs = new IntlListFormatter('es_ES', IntlListFormatter::TYPE_CONJUNCTION);
$formatterDe = new IntlListFormatter('de_DE', IntlListFormatter::TYPE_CONJUNCTION);

echo $formatterEs->format($languages); // Outputs: PHP, Python y JavaScript
echo $formatterDe->format($languages); // Outputs: PHP, Python und JavaScript
?>
            

This feature ensures lists are formatted correctly for different cultures, enhancing internationalization.

5. Supporting Right-to-Left Languages

The locale_is_right_to_left function and Locale::isRightToLeft method help detect RTL locales for proper UI rendering. Here’s an example:


<?php
$locale = 'ar_EG';
$isRtl = locale_is_right_to_left($locale);
echo $isRtl ? "Switch to RTL layout" : "Use LTR layout"; // Outputs: Switch to RTL layout

$localeObj = new Locale('fa_IR');
echo $localeObj::isRightToLeft() ? "RTL mode" : "LTR mode"; // Outputs: RTL mode
?>
            

This simplifies adapting interfaces for languages like Arabic or Persian.

6. Tracking Builds with PHP_BUILD_DATE

The PHP_BUILD_DATE constant provides the PHP build date, useful for version tracking. Here’s how you might use it:


<?php
echo "Running PHP built on: " . PHP_BUILD_DATE; // Outputs: e.g., Tue Aug 5 2025
$buildDate = DateTime::createFromFormat('M j Y H:i:s', PHP_BUILD_DATE);
echo "Build timestamp: " . $buildDate->format('Y-m-d'); // Outputs: e.g., 2025-08-05
?>
            

This is handy for logging or verifying PHP versions in deployment pipelines.

Conclusion

PHP 8.5 brings tools that make coding cleaner and more efficient, from the pipe operator’s elegant chaining to IntlListFormatter’s locale-sensitive formatting. These examples demonstrate how to apply these features in real projects. Experiment with PHP 8.5 alpha releases (e.g., PHP 8.5.0alpha4) to get ready for its November 2025 release. Visit PHP.Watch for updates and dive into these features to elevate your code!