1. <?php
  2. /**
  3. * Teardown function for environment variables.
  4. *
  5. * This function iterates through a predefined list of environment variables
  6. * and attempts to unset them. Includes error handling for cases where
  7. * the variable doesn't exist or unsetting fails.
  8. *
  9. * @param array $envVars An array of environment variable names to unset.
  10. * @return bool True on success, false on failure.
  11. */
  12. function teardownEnvironmentVariables(array $envVars): bool
  13. {
  14. $success = true;
  15. foreach ($envVars as $varName) {
  16. // Attempt to unset the environment variable.
  17. if (getenv($varName) !== false) { // Check if the variable exists
  18. unset($_ENV[$varName]); // Unset the variable from $_ENV (internal).
  19. // Optionally, unset from $_SERVER as well if needed.
  20. //unset($_SERVER[$varName]);
  21. } else {
  22. // Variable doesn't exist, log a warning (optional).
  23. error_log("teardownEnvironmentVariables: Variable '$varName' not found.");
  24. // Consider continuing to the next variable to avoid stopping the process.
  25. }
  26. }
  27. return $success;
  28. }
  29. //Example Usage (for testing)
  30. /*
  31. $varsToUnset = ['MY_INTERNAL_VAR', 'ANOTHER_VAR'];
  32. $result = teardownEnvironmentVariables($varsToUnset);
  33. if ($result) {
  34. echo "Environment variables teardown successful.\n";
  35. } else {
  36. echo "Environment variables teardown failed.\n";
  37. }
  38. */
  39. ?>

Add your comment