Can constants defined in another file be safely used for database connection details in PHP scripts?

It is generally safe to define constants in a separate file for database connection details in PHP scripts, as long as proper security measures are in place to protect the file containing sensitive information. To ensure security, the file containing the constants should be stored outside of the web root directory, and access should be restricted to prevent unauthorized users from viewing the file.

// db_config.php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_NAME', 'database_name');
```

```php
// index.php
require_once('db_config.php');

$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

echo "Connected successfully";