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

One common pitfall when working with arrays and objects in PHP is trying to access non-existent keys or properties, which can result in errors or unexpected behavior. To avoid this, always check if the key or property exists before trying to access it.

// Check if key exists before accessing it in an array
if (array_key_exists('key', $array)) {
    $value = $array['key'];
}

// Check if property exists before accessing it in an object
if (property_exists($object, 'property')) {
    $value = $object->property;
}