Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

string

php

I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.

I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.

like image 303
rob waminal Avatar asked Aug 10 '10 05:08

rob waminal


People also ask

What is the difference between single quote and double quote?

The main difference between double quotes and single quotes is, double quotes are used to denote a speech or a quotation whereas single quotes are used to indicate quote within a quotation.

What is the difference between strings with single quotes and double quotes in Javascript?

A double-quoted string can have single quotes without escaping them, conversely, a single-quoted string can have double quotes within it without having to escape them. Double quotes ( \" ) must escape a double quote and vice versa single quotes ( \' ) must escape a single quote.

Can we use both single quotes and double quotes for strings in PHP language?

In PHP, you can use both single quotes and double quotes ( " " ) for strings. True, but double quotes will expand PHP variables and escape sequences inside it. True, they are the same thing.

What is the difference between single quotes and double quotes when assigning it to a string variable?

Single quotes are used for regular expressions, dict keys or SQL. Double quotes are used for string representation.


1 Answers

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.

like image 196
Peter Ajtai Avatar answered Oct 21 '22 02:10

Peter Ajtai