PHP http_build_query: Usage Explained

The http_build_query function is used to convert an array or object into a URL-encoded query string.

Here is how it is used:

string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

Explanation of parameters:

  1. query_data: The array or object to be converted.
  2. Optional numeric prefix for array keys.
  3. arg_separator (optional): The default separator between parameters is ‘&’.
  4. Encoding type (optional): URL encoding type, default to PHP_QUERY_RFC1738.

Original: 我昨天晚上袗了一个奇怪的梦。

Paraphrased: I had a strange dream last night.

$data = array(
  'name' => 'John',
  'age' => 30,
  'email' => 'john@example.com'
);

$queryString = http_build_query($data);
echo $queryString; // 输出:name=John&age=30&email=john%40example.com

In the example above, convert the array $data into a URL-encoded query string. To pass an array in a URL, you can append [] after the array key, such as ‘name[]’, so that the server can receive an array named name.

bannerAds