<?php
/**
* Converts environment variables for debugging purposes with error logging.
*
* @param array $envVars An array of environment variables.
* @return array An array of converted environment variables.
*/
function convertEnvVars(array $envVars): array
{
$convertedVars = [];
$errors = [];
foreach ($envVars as $key => $value) {
try {
// Attempt to convert to string
$convertedVars[$key] = (string) $value;
} catch (\Exception $e) {
// Log the error
error_log("Error converting environment variable '$key': " . $e->getMessage());
$errors[$key] = "Conversion Error: " . $e->getMessage();
// Keep the original value if conversion fails
$convertedVars[$key] = $value;
}
}
if (!empty($errors)) {
error_log("Environment variable conversion errors occurred: " . print_r($errors, true));
}
return $convertedVars;
}
// Example Usage (for testing - remove or modify for your application)
/*
$envVars = getenv(); // Replace with your environment variable retrieval method
$converted = convertEnvVars($envVars);
print_r($converted);
*/
?>
Add your comment