How can $_SERVER['QUERY_STRING'] be properly utilized in PHP for URL manipulation?

The $_SERVER['QUERY_STRING'] variable in PHP can be used to access the query string parameters in a URL. This can be useful for URL manipulation, such as extracting specific parameters or modifying the query string dynamically. To properly utilize $_SERVER['QUERY_STRING'], you can parse the query string using PHP functions like parse_str() to extract and manipulate the parameters.

// Get the query string from the URL
$queryString = $_SERVER['QUERY_STRING'];

// Parse the query string into an associative array
parse_str($queryString, $params);

// Manipulate the parameters as needed
$params['new_param'] = 'value';

// Generate a new query string from the modified parameters
$newQueryString = http_build_query($params);

// Create the new URL with the modified query string
$newUrl = $_SERVER['PHP_SELF'] . '?' . $newQueryString;