nyholm/append-query-string

A simple function that lets you add a query string to an existing URL

Maintainers

👁 Nyholm

Package info

github.com/Nyholm/append_query_string

pkg:composer/nyholm/append-query-string

Fund package maintenance!

nyholm

Statistics

Installs: 289 454

Dependents: 2

Suggesters: 0

Stars: 19

Open Issues: 1

1.0.0 2021-08-16 01:31 UTC

Requires

  • php: ^7.2 || ^8.0

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 7aa854c9ac2db3a60f95ac9b4eae09ae876fb505

  • Nyholm <tobias.nyholm.woop@gmail.com>

This package is auto-updated.

Last update: 2026-06-20 15:44:42 UTC


README

If you have a unknown URL and want to add a query string to it, this package is what you are looking for.

Install

composer require nyholm/append-query-string

Usage

$url = 'https://nyholm.tech?example=yes';
$queryString = http_build_query(['foo'=>'bar']);

$result = append_query_string($url, $queryString);

echo $result;
// https://nyholm.tech?example=yes&foo=bar

Yes, this is pretty much as writing:

$result = $url . $queryString;

But it will support if URL has query string or not. It will also if a URL hash fragment is used.

Modes

There are three different modes you can use with append_query_string.

  • APPEND_QUERY_STRING_IGNORE_DUPLICATE (default)
  • APPEND_QUERY_STRING_REPLACE_DUPLICATE
  • APPEND_QUERY_STRING_SKIP_DUPLICATE

They are easiest explained with examples.

APPEND_QUERY_STRING_IGNORE_DUPLICATE

$url = 'https://nyholm.tech?foo=x&a=1';
$queryString = http_build_query(['a'=>'2']);

$result = append_query_string($url, $queryString, APPEND_QUERY_STRING_IGNORE_DUPLICATE);

echo $result;
// https://nyholm.tech?foo=x&a=1&a=2

APPEND_QUERY_STRING_REPLACE_DUPLICATE

$url = 'https://nyholm.tech?foo=x&a=1';
$queryString = http_build_query(['a'=>'2']);

$result = append_query_string($url, $queryString, APPEND_QUERY_STRING_REPLACE_DUPLICATE);

echo $result;
// https://nyholm.tech?foo=x&a=2

APPEND_QUERY_STRING_SKIP_DUPLICATE

$url = 'https://nyholm.tech?foo=x&a=1';
$queryString = http_build_query(['a'=>'2']);

$result = append_query_string($url, $queryString, APPEND_QUERY_STRING_SKIP_DUPLICATE);

echo $result;
// https://nyholm.tech?foo=x&a=1