/    Sign up×
Community /Pin to ProfileBookmark

http_build_query() Parameter Puzzles

Hi,

I don’t understand this.
The 4 echoes, all echo the same thing.

“`
<?php
$query_string = array(
“find” => “keyword”,
“tbl” => “links”,
“col” => “keyword”,
“max” => “100”
);

echo http_build_query($query_string); echo ‘<br>’;
echo http_build_query($query_string) . “n”; echo ‘<br>’;
echo http_build_query($query_string, ‘&’); echo ‘<br>’;
echo http_build_query($query_string, ”, ‘&’); echo ‘<br>’;
?>
“`

Q1. So, what’s the use of the latter 3 when the first echo is the simplest ?

Q2. And, what’s the use of adding the “&” on the 4th param as without it the url still contains the & as a separator separating the params ?

““
echo http_build_query($query_string, ‘&’); echo ‘<br>’;
““

Q3. And, why is the 3rd param here empty ?

““
echo http_build_query($query_string, ”, ‘&’); echo ‘<br>’;
““

Already checked:
https://www.php.net/manual/en/function.http-build-query.php

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 25.2021 — I almost hate to ask, but did you look at https://www.php.net/http_build_query ?

The 2nd and 3rd arguments do different things, with the 2nd one only doing anything for numerically indexed elements in the array supplied as the 1st argument (of which you have none, so it does nothing). The 3rd argument defines the separator between key/value pairs -- and if the default for your PHP configuration is to use &amp;, then stating it to that in the 3rd argument won't make any noticeable difference (but you might still want to use it if you want to be sure it uses that regardless of any changes to the PHP config). As far as appending a "n" after you echo it, that's purely cosmetic in the echo output -- it has nothing to do with what you're looking at here.
Copy linkTweet thisAlerts:
@developer_webauthorJul 25.2021 — @NogDog#1634704

Thanks! Your answer was worth it.

And yes, I did check it out but I do not understand things like ....
<i>
</i> http_build_query(
mixed $data,
string $numeric_prefix = ?,
string $arg_separator = ?,
int $encoding_type = PHP_QUERY_RFC1738
): string

The formula above is confusing. I call it formula, what's the right word for it ?

Unless you want to teach me here right now what that formula means then expect tonnes of questions which could have been avoided if I did understand these "formulas" that define the built-in functions in the manual.

I always look at the code samples. Not the formula as I don't understand it.

I had problem with these two lines:
<i>
</i>mixed $data,
string $numeric_prefix = ?,


**string $numeric_prefix = ?**

Now what on earth is that ?

**$mixed data** ? Meaning strings and ints etc allowed ? Different data types allowed ?

I'd rather just stick to the short default version:
<i>
</i>http_build_query($query_string);

And ofcourse, you would to.
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2021 — @NogDog#1634704

Thanks! Your answer was worth it.

And yes, I did check it out but I do not understand things like ....
<i>
</i> http_build_query(
mixed $data,
string $numeric_prefix = ?,
string $arg_separator = ?,
int $encoding_type = PHP_QUERY_RFC1738
): string

The formula above is confusing. I call it formula, what's the right word for it ?

Unless you want to teach me here right now what that formula means then expect tonnes of questions which could have been avoided if I did understand these "formulas" that define the built-in functions in the manual.

I always look at the code samples. Not the formula as I don't understand it.

I had problem with these two lines:
<i>
</i>mixed $data,
string $numeric_prefix = ?,


**string $numeric_prefix = ?**

Now what on earth is that ?

**$mixed data** ? Meaning strings and ints etc allowed ? Different data types allowed ?

I'd rather just stick to the short default version:
<i>
</i>http_build_query($query_string);

And ofcourse, you would to.

But don't forget to answer my question.

Cheers!
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...