What are best practices for ensuring that PHP files run smoothly across different environments?

When working with PHP files that need to run smoothly across different environments, it is important to avoid hardcoding absolute paths and configurations specific to a single environment. Instead, use relative paths and environment variables to dynamically adjust settings based on the environment.

// Example of using environment variables to set database connection settings
$host = getenv('DB_HOST') ?: 'localhost';
$username = getenv('DB_USERNAME') ?: 'root';
$password = getenv('DB_PASSWORD') ?: '';
$database = getenv('DB_NAME') ?: 'my_database';

// Use the variables in your database connection code
$connection = new mysqli($host, $username, $password, $database);