Are there any potential pitfalls when using the substr() function in PHP?

When using the substr() function in PHP, one potential pitfall is not checking the length of the string before extracting a substring. This can lead to errors if the starting position or length parameter is out of bounds. To avoid this issue, it is important to validate the input parameters to ensure they are within the bounds of the original string.

$string = "Hello, World!";
$start = 7;
$length = 5;

if ($start >= 0 && $length > 0 && $start + $length <= strlen($string)) {
    $substring = substr($string, $start, $length);
    echo $substring;
} else {
    echo "Invalid parameters for substring extraction.";
}