What is the difference between single quote and double quote string in PHP?

PHP single quotes execute slight faster than double quotes but single quote does not parse variables. Here is the difference between single quote and double quote string in PHP:

Single quotes

  1. The simplest method to declare a string is using single quotes. They are faster because everything written inside single quotes is treated as plain string.
  2. This is useful when we need to output exactly as it is written in single quotes. If we place a variable inside single quotes, it will output as a same variable name but not its value.
  3. Using single quoted string, PHP will not evaluate most escaped characters except single quote with backslash (\’). For an example, If we want to output single quoted word inside single quoted string: echo ‘This is \’sample\’ string’;

Double quote

  1. It will parse various escaped characters, regular expressions, and variables in the strings.
  2. We can also use curly braces to wrap variables if additional word needs to attach with variable value output. For example if we have variable name $number (with its value 100) and you what to echo “The 100s kids are there”. You can wrap variable with curly braces such as: “The {$number}s kids are there”.
  3. Because of parsing escaped characters & variables, this is slower than single quoted string.

Note: If there is no variable or escaped characters in string, use single quote else double quote.


Learn more about the similar topics?
Tutorials
  • No Content Found.
  • Interview Questions & Answers
  • No Content Found.
  • Exercises & Assignments
  • No Content Found.