Skip to content
PHP Logo

How can PHP developers prevent duplicate parameters from being added to a URL when using $_GET?

When using $_GET to retrieve parameters from a URL in PHP, developers can prevent duplicate parameters from being added by checking if the parameter already exists before adding it. This can be done by storing the parameters in an array and checking if the parameter exists before adding it to the array.

php
// Initialize an empty array to store parameters
$params = [];

// Check if the parameter already exists before adding it
if (!isset($params['param'])) {
    $params['param'] = $_GET['param'];
}

// Build the new URL with the unique parameters
$newUrl = 'http://www.example.com?' . http_build_query($params);