What are some common pitfalls when working with arrays and strings in PHP?

One common pitfall when working with arrays in PHP is not properly checking if a key exists before trying to access it, which can result in "Undefined index" errors. To avoid this issue, you can use the isset() function to check if a key exists before attempting to access it.

// Check if a key exists before accessing it in an array
$array = ['key1' => 'value1', 'key2' => 'value2'];

if (isset($array['key1'])) {
    echo $array['key1'];
} else {
    echo 'Key does not exist';
}