What are some common pitfalls when working with PHP arrays and string manipulation?

One common pitfall when working with PHP arrays and string manipulation is not properly handling empty or null values, which can lead to errors or unexpected behavior. To solve this, always check if the array key exists before accessing it and validate string inputs before performing any manipulations.

// Check if array key exists before accessing it
if(isset($array['key'])) {
    // Perform string manipulation
    $result = strtoupper($array['key']);
} else {
    $result = "Key does not exist";
}

// Validate string input before manipulation
$string = "example";
if(!empty($string)) {
    // Perform string manipulation
    $result = strtolower($string);
} else {
    $result = "String is empty";
}