What are some common pitfalls when using substr() in PHP for string manipulation?
One common pitfall when using substr() in PHP for string manipulation is not considering the starting index and length parameters correctly. If the starting index is negative, it counts from the end of the string. If the length parameter is negative, it counts that many characters from the end of the string. To avoid errors, always double-check the starting index and length values when using substr().
// Example of using substr() with correct starting index and length parameters
$string = "Hello, World!";
$substring = substr($string, 7, 5); // Outputs "World"
echo $substring;