What best practices should be followed when declaring and using constants in PHP scripts to avoid unexpected behavior?

When declaring and using constants in PHP scripts, it is important to follow best practices to avoid unexpected behavior. One common mistake is not using the 'define()' function to declare constants, which can lead to redeclaration errors or unintended variable behavior. To avoid these issues, always use the 'define()' function to declare constants and ensure that they are defined before being used in the script.

// Define constants using the define() function
define('MAX_LOGIN_ATTEMPTS', 3);
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');

// Using constants in the script
if($loginAttempts >= MAX_LOGIN_ATTEMPTS) {
    echo "Maximum login attempts exceeded.";
}

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS);