The `substr()` function in PHP is used to extract a substring from a string. It hasn't changed significantly in PHP 8.2, but it's still one of the most commonly used string functions.PHP 8.PHP 8.1,PHP 8.2,PHP 8.3 and PHP 8.4.
Basic Syntax<?php substr(string $string, int $start, ?int $length = null): string ?>
Examples Basic Usage<?php$str = "Hello, World!";echo substr($str, 0, 5); // Outputs: Helloecho substr($str, 7); // Outputs: World!echo substr($str, -6); // Outputs: World!echo substr($str, -6, 3); // Outputs: Worecho substr($str, 7, -1); // Outputs: World?>
Edge Cases<?php$str = "Short";// Start position exceeds string lengthecho substr($str, 10); // Outputs: (empty string)// Negative start position exceeds string lengthecho substr($str, -10); // Outputs: "Short" (treated as position 0)?>
The `substr()` function remains a reliable way to extract portions of strings in PHP 8.2, though for multibyte character encodings, `mb_substr()` is recommended.