What are some best practices for securely handling keys in PHP scripts, especially when interacting with APIs?

When handling keys in PHP scripts, especially when interacting with APIs, it is crucial to keep them secure to prevent unauthorized access. One best practice is to store keys in environment variables or configuration files outside of the web root directory. This helps prevent exposure of sensitive information in case of a security breach.

// Store API key in environment variable
$apiKey = getenv('API_KEY');

// Or store API key in a separate configuration file
$config = include('config.php');
$apiKey = $config['api_key'];