How can the use of constants in PHP functions lead to errors in PHP 8.1?

Using constants in PHP functions can lead to errors in PHP 8.1 because constants are resolved at compile time, leading to potential issues when the constant is not defined or if it is defined after the function is declared. To solve this issue, you can use the `defined()` function to check if the constant is defined before using it in the function.

// Check if the constant is defined before using it in the function
function myFunction() {
    if (defined('MY_CONSTANT')) {
        // Use the constant here
        echo MY_CONSTANT;
    } else {
        echo "Constant MY_CONSTANT is not defined.";
    }
}

// Define the constant
define('MY_CONSTANT', 'Hello, World!');

// Call the function
myFunction();