Oihana PHP

formatRequestArgs.php

Table of Contents

Functions

formatRequestArgs()  : string
Builds a query string (`?key=value&...`) from an associative array.

Functions

formatRequestArgs()

Builds a query string (`?key=value&...`) from an associative array.

formatRequestArgs(array<string|int, mixed> $params[, bool $useNow = false ]) : string

Optionally replaces the value of the from key with "now" if $useNow is set to true. This is useful for formatting request parameters in URLs or APIs.

Parameters
$params : array<string|int, mixed>

The associative array of parameters (e.g., ['from' => 'yesterday', 'limit' => 10]).

$useNow : bool = false

Whether to override the 'from' key with 'now' if it exists.

Tags
example
echo formatRequestArgs(['from' => '2023-01-01', 'limit' => 10]);
// Output: "?from=2023-01-01&limit=10"

echo formatRequestArgs(['from' => 'yesterday', 'limit' => 5], true);
// Output: "?from=now&limit=5"

echo formatRequestArgs([]);
// Output: ""

echo formatRequestArgs(['search' => 'php functions', 'page' => 2]);
// Output: "?search=php functions&page=2"
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

A URL-compatible query string, starting with ?, or an empty string if $params is empty.


        
On this page

Search results