How can function_exists and class_exists help in ensuring code compatibility and safety when working with external code?

When working with external code in PHP, it's important to ensure compatibility and safety. One way to do this is by using the function_exists and class_exists functions to check if a function or class exists before calling it. This helps prevent errors and conflicts with undefined functions or classes in external code.

if (function_exists('external_function')) {
    // Call the external function
    external_function();
}

if (class_exists('ExternalClass')) {
    // Instantiate the external class
    $external_object = new ExternalClass();
}