25

My question is related to this one. except that my question is more sepcific as it is about whether a hyphen can be used in a query string parameter value.

I am parsing $_SERVER['QUERY_STRING'] with PHP. I would like to know whether it is syntactically correct to use hyphens in query string values such as in the following case, or whether hyphens must be escaped in the browser URL. What about underscores?

http://example.com/?q1=query-string-value-one&q2=query-string-value-two

According to this document hyphens should be OK in all standards-compliant browsers, but I wanted to double check.

Thanks.

6
  • 5
  • 4
    Well, I don't just want to know if it works. I need to know whether it is legal according to RFC specs. Sep 2, 2015 at 9:05
  • 3
    yes hyphens can be used . only characters can reliably use for the actual name parts of a URL are a-z, A-Z, 0-9, -, ., _, and ~.
    – Bender
    Sep 2, 2015 at 9:09
  • 2
    possible duplicate of Safe characters for friendly url
    – Hans Z.
    Sep 2, 2015 at 9:17
  • 1
    I don't think this is a duplicate @HansZ. The question you link to is concerned about legal characters in a url in general. This question is concerned with how query parameter names are parsed.
    – seebiscuit
    Apr 6, 2020 at 14:57

2 Answers 2

29

You are talking about query string parameters which must be encoded using urlencode function:

This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.

According to the documentation - does not require encoding.

16

Yes

Hyphens can be used for query string parameter names

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.