How can errors related to illegal string offsets and invalid arguments in foreach loops be resolved in PHP?

Illegal string offsets occur when trying to access a character in a string using an index that is not a valid integer. To resolve this, you can check if the index exists in the string before accessing it. Invalid arguments in foreach loops can be resolved by ensuring that the argument passed to the loop is iterable, such as an array or object implementing the Traversable interface.

// Illegal string offset fix
$string = "Hello";
$index = 2;
if(isset($string[$index])) {
    echo $string[$index];
}

// Invalid argument in foreach loop fix
$data = [1, 2, 3];
if(is_array($data) || $data instanceof Traversable) {
    foreach($data as $value) {
        echo $value;
    }
}