<?php
/**
* Teardown function for environment variables.
*
* This function iterates through a predefined list of environment variables
* and attempts to unset them. Includes error handling for cases where
* the variable doesn't exist or unsetting fails.
*
* @param array $envVars An array of environment variable names to unset.
* @return bool True on success, false on failure.
*/
function teardownEnvironmentVariables(array $envVars): bool
{
$success = true;
foreach ($envVars as $varName) {
// Attempt to unset the environment variable.
if (getenv($varName) !== false) { // Check if the variable exists
unset($_ENV[$varName]); // Unset the variable from $_ENV (internal).
// Optionally, unset from $_SERVER as well if needed.
//unset($_SERVER[$varName]);
} else {
// Variable doesn't exist, log a warning (optional).
error_log("teardownEnvironmentVariables: Variable '$varName' not found.");
// Consider continuing to the next variable to avoid stopping the process.
}
}
return $success;
}
//Example Usage (for testing)
/*
$varsToUnset = ['MY_INTERNAL_VAR', 'ANOTHER_VAR'];
$result = teardownEnvironmentVariables($varsToUnset);
if ($result) {
echo "Environment variables teardown successful.\n";
} else {
echo "Environment variables teardown failed.\n";
}
*/
?>
Add your comment