What is the best practice for structuring an if statement in PHP to ensure that the comparison values are not evaluated if the key does not exist in the array?

When using an if statement in PHP to check if a key exists in an array before comparing its value, it is important to first verify the existence of the key to avoid potential errors. One way to achieve this is by using the isset() function to check if the key exists in the array before attempting to access its value for comparison. This ensures that the comparison values are only evaluated if the key exists in the array, preventing any undefined index errors.

// Example of structuring an if statement to avoid undefined index errors
$array = ['key1' => 'value1', 'key2' => 'value2'];

if (isset($array['key1']) && $array['key1'] == 'value1') {
    // Perform actions if key1 exists and its value is 'value1'
    echo 'Key1 exists and its value is value1';
} else {
    // Handle case where key1 does not exist or its value is not 'value1'
    echo 'Key1 does not exist or its value is not value1';
}