What is the difference between single quote and double quote string in PHP?
In PHP a string can be written in single quotes or double quotes. They work differently:
| Single quotes (‘ ‘) | Double quotes (” “) |
|---|---|
| Text is used exactly as written | Variables inside are replaced with their values |
Only \' and \\ are special | Escape codes like \n, \t and \" work |
| Good for plain text | Good when you need variables inside the text |
<?php
$name = "Robin";
echo 'Hello $name' . "<br>";
echo "Hello $name" . "<br>";
echo "Price: {$name}s";
?>
Output:
Tutorials Class - Output Window
Hello $name
Hello Robin
Price: Robins
The curly braces in the last line mark where the variable name ends.
Note:
You may read that single quotes are faster. The difference is so tiny that you will never notice it, so choose by need, not by speed. For long text on many lines use heredoc (like double quotes) or nowdoc (like single quotes). See PHP String Handling.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |