What are common mistakes in PHP code that may lead to errors like "Illegal string offset"?

The "Illegal string offset" error in PHP occurs when trying to access an index of a string as if it were an array. This error typically happens when using square brackets to access characters in a string instead of using functions like substr(). To fix this issue, make sure to use string functions to manipulate strings instead of treating them as arrays.

// Incorrect usage leading to "Illegal string offset" error
$string = "Hello";
echo $string[0]; // Trying to access string as an array

// Correct way to access characters in a string
$string = "Hello";
echo substr($string, 0, 1); // Using substr() function to access first character