Backslashes in Single Quoted Strings Vs. Double Quoted Strings

Backslashes in single quoted strings vs. double quoted strings

I'd refer you to "Ruby Programming/Strings" for a very concise yet comprehensive overview of the differences.

From the reference:

puts "Betty's pie shop"

puts 'Betty\'s pie shop'

Because "Betty's" contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is in the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an escape sequence.

What is the difference between single-quoted and double-quoted strings in PHP?

PHP strings can be specified not just in two ways, but in four ways.

  1. Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
  2. Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
  3. Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
  4. Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.

Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:

$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";

Speed:

I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations toward the bottom, section C). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.

Single quoted string vs. double quoted string

It doesn't matter if you use ' or " around the string to mark it as string literal. But you can't use that character inside the string literal without escaping it using a \ in front of it - otherwise Python interprets it as the end of the string.

For example " inside a " delimited string literal need to be escaped as well:

a = "And he said: \"What a nice day\"."

R character/string: '...' vs ...

From ?"'":

Details

Three types of quotes are part of the syntax of R: single and double
quotation marks and the backtick (or back quote, `). In addition,
backslash is used to escape the following character inside character
constants.

Character constants

Single and double quotes delimit character constants. They can be used
interchangeably but double quotes are preferred
(and character
constants are printed using double quotes), so single quotes are
normally only used to delimit character constants containing double
quotes.

Backslash is used to start an escape sequence inside character
constants. Escaping a character not in the following table is an
error.

Single quotes need to be escaped by backslash in single-quoted
strings, and double quotes in double-quoted strings.

Difference Between Single and Double Quoted Strings in ActionScript

You can use either as delimiter for a string. They are however not interchangeable, i.e. you can't start a string with an apostrophe and end it with a quotation mark.

The only difference is which characters you need to escape. Inside a string delimited by quotation marks you need to escape quotation marks but not apostrophes, and vice versa.

To put the text He said "It's all right" and laughed. in a string you can use:

"He said \"It's all right\" and laughed."

or:

'He said "It\'s all right" and laughed.'

Why is a double-backslash escape required inside single-quoted strings?

In order to end a String with a backslash

The one escape initially needed in hard-quoted strings is \', as others also note.

But the real, fundamental reason why \\ is also needed is because, once \' is supported, it otherwise would not be possible to end a hard-quoted string with a backslash.

Extracting double quoted strings with escape sequences

If you echo your pattern, you'll see it's indeed passed as %"(?:\"|.)*?"% to the regex parser. The single backslash will be treated as an escape character even by the regex parser.

So you need to add at least one more backslash if the pattern is inside single quotes to pass two backslashes to the parser (one for escaping backlsash) that the pattern will be: %"(?:\\"|.)*?"%

preg_match_all('%"(?:\\\"|.)*?"%', $msg, $matches);

Still this isn't a very efficient pattern. The question seems actually a duplicate of this one.

There is a better pattern available in this answer (what some would call unrolled).

preg_match_all('%"[^"\\\]*(?:\\\.[^"\\\]*)*"%', $msg, $matches);

See demo at eval.in or compare steps with other patterns in regex101.



Related Topics



Leave a reply



Submit