Why does \ t avoid this PHP date format?

Here is the date. Everything escapes, but t is in "\ a \ t". Does anyone know why?

date("M m\, Y \a\tg\:ia", $s->post_date); 
+4
source share
1 answer

"\t" is the escape sequence for the horizontal tab character.

Use '\t' or "\\t"

Single quoted strings interpret \ literally, which I would recommend for your use case. Otherwise, you need to escape the \ character so that it is interpreted literally.

In the case of PHP, \ , the preceding invalid escape sequence inside a double-quoted string is also interpreted literally. I would rather avoid this behavior, following the principle of least surprise.

ps. (thanks to @IMSoP) There are two cases where \ not interpreted literally inside single quote strings:

  • Doubling backslashes is still possible, but not necessary. For example: '\\hi' === '\hi'
  • The string delimiter character must be escaped inside the string literal. For example: '\'' === "'"

However, one-line strings are less surprising in that \n , \r , \t , \v , \040 and a similar result in the actual sequence of characters inside a string literal instead of those interpreted as escape sequences.

Doubling all backslashes, which must be interpreted literally, is also a strong option that works with both double and single quotes.

+10
source

All Articles