What is the difference between single quote and double quote string in PHP?
PHP single quotes execute slightly faster than double quotes but a single quote does not parse variables. Here is the difference between a single quote and double quote string in PHP:
Single quotes
- The simplest method to declare a string is using single quotes. They are faster because everything written inside single quotes is treated as a plain string.
- 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 the same variable name but not its value.
- Using single-quoted string, PHP will not evaluate most escaped characters except a single quote with a backslash
(\')
. For example, If we want to output single quoted word inside single-quoted string:echo 'This is \' sample\' string';
Double quote
- It will parse various escaped characters, regular expressions, and variables in the strings.
- 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"
. - Because of parsing escaped characters & variables, this is slower than single quoted string.
Note:
Note: If there is no variable or escaped characters in the string, use a single quote else double quote.
Learn more about the similar topics:
Tutorials |
---|
No Content Found. |
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |