What are some potential pitfalls when using the substr function in PHP?

One potential pitfall when using the substr function in PHP is not considering the character encoding of the string being manipulated. This can lead to unexpected results, especially when dealing with multibyte characters. To avoid this issue, it is important to use mb_substr instead of substr when working with multibyte strings.

// Incorrect usage of substr with multibyte characters
$string = "こんにちは";
$substring = substr($string, 0, 3); // Incorrect result due to multibyte characters

// Correct way to use mb_substr with multibyte characters
$substring = mb_substr($string, 0, 3, 'UTF-8');
echo $substring; // Output: こん