1. <?php
  2. /**
  3. * Converts environment variables for debugging purposes with error logging.
  4. *
  5. * @param array $envVars An array of environment variables.
  6. * @return array An array of converted environment variables.
  7. */
  8. function convertEnvVars(array $envVars): array
  9. {
  10. $convertedVars = [];
  11. $errors = [];
  12. foreach ($envVars as $key => $value) {
  13. try {
  14. // Attempt to convert to string
  15. $convertedVars[$key] = (string) $value;
  16. } catch (\Exception $e) {
  17. // Log the error
  18. error_log("Error converting environment variable '$key': " . $e->getMessage());
  19. $errors[$key] = "Conversion Error: " . $e->getMessage();
  20. // Keep the original value if conversion fails
  21. $convertedVars[$key] = $value;
  22. }
  23. }
  24. if (!empty($errors)) {
  25. error_log("Environment variable conversion errors occurred: " . print_r($errors, true));
  26. }
  27. return $convertedVars;
  28. }
  29. // Example Usage (for testing - remove or modify for your application)
  30. /*
  31. $envVars = getenv(); // Replace with your environment variable retrieval method
  32. $converted = convertEnvVars($envVars);
  33. print_r($converted);
  34. */
  35. ?>

Add your comment