In the provided PHP code snippet, what is the purpose of the defined() function and how does it prevent hacking attempts?

The defined() function in PHP is used to check if a constant has been defined. By using defined() to check if a critical constant, such as a database connection details constant, is defined before accessing it, we can prevent potential hacking attempts that exploit vulnerabilities by directly accessing sensitive information.

if(defined('DB_HOST') && defined('DB_USER') && defined('DB_PASS') && defined('DB_NAME')) {
    // Proceed with database connection using the defined constants
} else {
    // Handle the case where the critical constants are not defined
}