What Is the Meaning of ? (Question Mark) in a Url String

What is the meaning of ? (question mark) in a URL string?

Its name is query string. After the question mark you can pass key-value pairs and use them server-side.

What do a question mark (?) and an ampersand (&) mean in a URL?

There isn't really anything there that is particular to PHP.

The ? indicates the start of the query string. Within the query string you have a set of key=value pairs, each separated by an &.

PHP will populate $_GET with this data. It is part of the URL standard and any server side language will have a parser that provides similar functionality.

This is also the default data format browsers generate when submitting a form.

What's the term for the part of the URL after the question mark?

It's the query, or sometimes the query string.

To pinch a useful diagram from the URI RFC:

     foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment

What is the name of the question mark in my URL?

In this document, it's literally called the "question-mark" (hyphen included) (http://www.ietf.org/rfc/rfc2396.txt -- scroll down to 3.2 Authority Component).

I agree it's a bad name. The hash (#) is called the "Fragment Identifier" (scroll down to 4.1). So maybe it should be called the "Query Identifier".

Is question mark in URL part of query string?

tl;dr:

  • The ? is not part of the query component.
  • The URIs are not equivalent, because one has no query component, while the other one has an empty query component.

The URI standard (STD 66) is currently RFC 3986.

Section 6.2. Comparison Ladder describes methods how to test if URIs are possibly equivalent.

In 6.2.3. Scheme-Based Normalization it says:

Normalization should not remove delimiters when their associated component is empty unless licensed to do so by the scheme specification. For example, the URI http://example.com/? cannot be assumed to be equivalent to any of the examples above.

Where "examples above" refers to:

http://example.com
http://example.com/
http://example.com:/
http://example.com:80/

(These 4 URIs are equivalent.)

So http://example.com/api/item.json has no query component, while http://example.com/api/item.json? has an empty query component.

Is it valid to have more than one question mark in a URL?

Yes, it is valid. Only the first ? in a URL has significance, any after it are treated as literal question marks:

The query component is indicated by
the first question mark ("?")
character and terminated by a number
sign ("#") character or by the end of
the URI.

...

The characters slash ("/") and
question mark ("?") may represent data
within the query component. Beware
that some older, erroneous
implementations may not handle such
data correctly when it is used as the
base URI for relative references
(Section 5.1), apparently because they
fail to distinguish query data from
path data when looking for
hierarchical separators. However, as
query components are often used to
carry identifying information in the
form of "key=value" pairs and one
frequently used value is a reference
to another URI, it is sometimes better
for usability to avoid
percent-encoding those characters.

https://www.rfc-editor.org/rfc/rfc3986#section-3.4

Can a valid URL usefully contain more than 1 question mark?

I think this is a great question and has been answered here.

Obviously, it is not something common that we usually see (some places even say that this should be not allowed), but I'm going to show a few more practical examples of that. Even though it is not common, I believe that "preventing URLs" with two question marks may not be so good and in the end, I'll say why.

Simple URL redirect service

Let's say that some redirection service has a code more or less like this:

<?php

if ($_GET['redirect_to']) {
$url = $_GET['redirect_to'];
if (filter_var($url, FILTER_VALIDATE_URL)) {
//valid URL, redirect
header("HTTP/1.1 302 Moved Temporarily");;
header("Location: " . $url);
die;
}
}

As you can see, the code first validates the URL, if valid, redirect the user. If you access this, it will work:

http://localhost/?redirect_to=https://www.example.com/content?from=https://api.example.com/posts?id=2

There are multiple question marks there, one to define the redirect_to parameter, another one to the from parameter, and the last to the id parameter of the redirected URL. To PHP, all this (redirect_to content) is a valid URL. Also, the StackOverflow editor highlights the whole URL as a valid link (the generated answer does not, but I will keep it this way and you can check that if try to edit).

API's

You can download the JSON Placeholder API and create a new post on data.json like this:

{
"userId": 1,
"id": 1,
"title": "Is multiple question marks useful??",
"body": "Maybe"
}

After running with npm start, you can access http://localhost:3000/posts?title=Is%20multiple%20question%20marks%20useful?? and the post will be returned, so, this would be an example where someone would like to return a post that contains the question mark.

HOWEVER

It's pretty obvious that using multiple question marks on the URL is very weird, and not all clients can interpret this correctly, so it might also be a bad practice to build a system that generates this kind of URL. But your question is about to validate or not multiple question marks, so, thinking that those URLs can belong to other services that you don't have control, to me, there's no need to do the validation.

What is the question mark for in a Typescript parameter name

It is to mark the parameter as optional.

  • TypeScript handbook https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters
  • TypeScript Deep Dive https://basarat.gitbook.io/typescript/type-system/functions#optional-parameters


Related Topics



Leave a reply



Submit