Greg said:
> I've got a few php pages that are invoked like this:
>
>
http://...domain.../sample.php?parm1=xxx&parm2=yyy&parm3=zzz
>
> Sometimes though, I end up with this:
>
>
http://...domain.../sample.php?&parm2=yyy
What do you mean by, "I end up with"?
> ^^
> Although this seems to work as far as I could test it, it is maybe not
> as clean as it should be.
Right.
> I could add some more code to clean it up. My question is, do I have
> to? Why?
It's kind of hard to guess what sort of explanation will convince
someone to fix broken code who doesn't think it's obvious why it's a
good idea! Especially when the fix is as simple as replacing "?&" with
"?". Or, even better, as simple as not building the error in in the
first place. I'm guessing that your original is something like the
following pseudo-code:
url = "
http://example.com/sample.php?"
if (shouldInclude(parm1))
url = url + parm1.name + "=" + parm1.value;
if (shouldInclude(parm2))
url = url + "&" + parm2.name + "=" + parm2.value;
if (shouldInclude(parm3))
url = url + "&" + parm3.name + "=" + parm3.value;
It doesn't take much more work to produce:
iIncludedParms = 0;
url = "
http://example.com/sample.php"
for each parm in possibleParms
{
if (shouldInclude(parm))
{
if (iIncludedParms == 0)
url = url + "?" + parm.name + "=" + parm.value;
else
url = url + "&" + parm.name + "=" + parm.value;
iParms = iParms + 1;
}
}
This coding pattern comes up again and again (example: insertion of
"AND" when dynamically adding criteria to the WHERE clause in a SQL
command based on a user's filter selection, or, likewise, insertion of
commas when dynamically adding sort columns to the ORDER BY clause), so
you ought to be familiar with it.
By the way, if this URL you are generating is meant to be the value of
an HREF or SRC attribute in the generated HTML, use "&" instead of "&".