Skip to content
PHP Logo

How can multiple GET variables be attached to a URL in PHP?

To attach multiple GET variables to a URL in PHP, you can use the `http_build_query` function to build a query string with the key-value pairs of the variables you want to pass. This function takes an array of parameters and returns a URL-encoded string. You can then append this string to the URL with a question mark (?) if it's the first parameter or an ampersand (&) if there are already existing parameters.

php
// Define the GET variables as an associative array
$parameters = array(
    'variable1' => 'value1',
    'variable2' => 'value2'
);

// Build the query string
$queryString = http_build_query($parameters);

// Append the query string to the URL
$url = 'http://example.com/page.php?' . $queryString;

echo $url;