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 escape sequences (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". 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:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this

for(i<100000;$i++) {
    'string';
}

The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.

Answer from Peter Ajtai on Stack Overflow
🌐
PHP
php.net › manual › en › language.types.string.php
PHP: Strings - Manual
Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. ... <?php echo 'this is a simple string', PHP_EOL; echo 'You can also have embedded newlines in strings this way as it is okay to do', PHP_EOL; // Outputs: Arnold once said: "I'll be back" echo 'Arnold once said: "I\'ll be back"', PHP_EOL; // Outputs: You deleted C:\*.*? echo 'You deleted C:\\*.*?', PHP_EOL; // Outputs: You deleted C:\*.*? echo 'You deleted C:\*.*?', PHP_EOL; // Outputs: This will not expand: \n a newline echo 'This will not expand: \n a newline', PHP_EOL; // Outputs: Variables do not $expand $either echo 'Variables do not $expand $either', PHP_EOL; ?>
🌐
W3Schools
w3schools.com › php › php_string.asp
PHP Strings
// Using double quotes $x = "John"; echo "Hello $x\n"; echo "\tHow are you?\n"; // Using single quotes $x = 'John'; echo 'Hello $x\n'; echo '\tHow are you?\n'; Try it Yourself » · For a complete reference of all string functions, go to our complete PHP String Reference.
Discussions

How to include single quotes in the value of a text string - PHP - SitePoint Forums | Web Development & Design Community
Hi I have a script that writes values to a php text file that works almost fine as in :- $content = '$security_code = '. $short_string . ';'."\n"; fwrite($txtfile, $content); and writes a line to a text file as $security_code = BA1EF7; Only problem is I want it to write to the file as follows ... More on sitepoint.com
🌐 sitepoint.com
0
February 15, 2021
PHP double quote/single quote/string/array interaction
Not sure what the question is, but see https://www.php.net/manual/en/language.types.string.php Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. . = concatenation... it joins two strings 'foo' . 'bar' results in "foobar" I know that . is for concatenation can you mind explaining the purpose of the dots and the quote marks? you're building the javascript array echo "[ '".$row['gender']."' , ".$row['number']." ] ,"; outputs ['male', 42], ( "['" + gender + "', " + number + "]," ) maybe do echo json_encode(array($row['gender'], $row['number'])); instead More on reddit.com
🌐 r/PHPhelp
7
0
June 22, 2021
Question: Why do PHP developers hate sprintf()?
I think most people disagree that sprintf() is more concise, and instead view it as unneeded complexity. $feelThings = $goodEmotion ? "happy" : "sad"; $s = "My $spouse has $petNumber $petType and that makes me $feelThings"; Kinda like disliking ternary operators within other constructs...personal preference! More on reddit.com
🌐 r/PHPhelp
30
12
January 5, 2022
PHP syntax highlighting between HTML quotes
But i just can’t seem to find how it’s possible to get the right PHP syntax highlighting when working with a PHP / HTML combination. And that’s kind of important. One may use PHP code inside HTML quotes like in the screenshot, but the the PHP syntax isn’t right anymore. The screenshot ... More on forum.sublimetext.com
🌐 forum.sublimetext.com
0
0
March 8, 2013
Top answer
1 of 7
1252

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 escape sequences (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". 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:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this

for(i<100000;$i++) {
    'string';
}

The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.

2 of 7
256

Things get evaluated in double quotes but not in single:

$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
🌐
TinyMCE
tiny.cloud › blog › php-escape
How to work with PHP escape quotes and characters | TinyMCE
December 7, 2023 - Master PHP escape sequences with our guide. Learn string escape basics, single vs double quote usage, and how to navigate heredoc syntax.
🌐
Droptica
droptica.com › blog › combining-string-literals-and-variables-php
Combining strings and variables in PHP 8+: examples and common mistakes
PHP gives you four common ways to build dynamic strings: concatenation with single quotes, interpolation inside double quotes, sprintf() formatting and heredoc/nowdoc blocks. Each has different readability, escaping rules and maintenance cost.
🌐
DEV Community
dev.to › realflowcontrol › too-double-quote-or-not-thats-the-question-78l
To double quote or not, that's the question! - DEV Community
August 23, 2024 - This feature is limited to strings in double quotes and heredoc. Using single quotes (or nowdoc) will yield a different result: $juice = "apple"; echo 'They drank some $juice juice.'; // will output: They drank some $juice juice. Look at that: PHP will not search for variables in that single quoted string.
Find elsewhere
🌐
CodeBasics
code-basics.com › programming › php course › quotes
CodeBasics | Quotes | PHP
In other words, anything inside quotes is considered a string, even if it's just a space or nothing at all. If you display strings on the screen, 'Hello' and 'Goodbye' will be clearly visible. But ' ' and '' can be confusing, because printing an empty string looks like a complete absence of output, while a string with a space shows "empty space" that is visually hard to tell apart. PHP, however, clearly distinguishes between them.
🌐
GeeksforGeeks
geeksforgeeks.org › php › what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php
What is the difference between single-quoted and double-quoted strings in PHP? - GeeksforGeeks
April 10, 2022 - Double-quoted strings: By using Double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences.
🌐
SitePoint
sitepoint.com › php
How to include single quotes in the value of a text string - PHP - SitePoint Forums | Web Development & Design Community
February 15, 2021 - Hi I have a script that writes values to a php text file that works almost fine as in :- $content = '$security_code = '. $short_string . ';'."\n"; fwrite($txtfile, $content); and writes a line to a text file as $security_code = BA1EF7; Only problem is I want it to write to the file as follows $security_code = 'BA1EF7'; ie value enclosed in single quotes - any suggestions please
🌐
Reddit
reddit.com › r/phphelp › php double quote/single quote/string/array interaction
r/PHPhelp on Reddit: PHP double quote/single quote/string/array interaction
June 22, 2021 -

I've been fiddling google charts for a while now for my project, watching youtube videos trying to copy and learn how to do it.

The problem was my chart will only show "Other" and only a grey, 100% pie instead of showing the Male/Female division from my database table.

I somehow got into the conclusion that the chart wont read the "number" from the query, since I used an AS statement on my SQL line, so I tried to search for solutions on how to transform the array into int but I dropped since they seemed to be complex and I was just starting how to get here.

I've been stuck for days but i finally solved it when I decided to abandon the learning for a bit and straight out copy the code from the video, and I noticed that the row[number] part did not need the enclosure of single quotes and it worked like magic.

So somehow the removal of the single quotes "freed" the number and it displayed the chart. What is exactly the explanation behind this? The syntax overwhelmed me a bit cause I was learning JS, HTML, CSS, and PHP all at the same time as I progress. I know in PHP you can use "" or '' for strings, I know that . is for concatenation, but this one uses dots to enclose the array. Or not. Or maybe it was a javascript syntax from the google chart api. Can someone enlighten me about this?

var data = google.visualization.arrayToDataTable([
        ['gender', 'number'],
        <?php
            while($row = mysqli_fetch_array($result))
            {
                echo "[ '".$row['gender']."' , ".$row['number']." ] ,";
            }   
        ?>    
        ]);

// tldr: i used to put ".$row['number']." inside '', like the row gender
and it didnt work. can you mind explaining the purpose of the dots and the
quote marks?
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-work-with-strings-in-php
How To Work with Strings in PHP | DigitalOcean
A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. In this article, you will learn how to create and view the…
🌐
Medium
mderis.medium.com › the-great-debate-or-in-php-best-practices-and-examples-45c614b1303e
The Great Debate: ‘ or “ in PHP — Best Practices and Examples | by Moslem Deris | Medium
May 29, 2023 - In conclusion, the decision to use single or double quotes in PHP ultimately depends on the specific requirements of your code. When defining simple string literals that do not require variable substitution or complex escape sequences, it’s best to use single quotes.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-strings
PHP Strings - GeeksforGeeks
April 11, 2025 - You can create a string using single quotes (' ') or double quotes (" "). PHP supports special syntax like heredoc and nowdoc for multiline strings.
🌐
Affinity
forum.affinity.serif.com › home › affinity support › feedback & suggestions › feedback for the affinity v1 products [archive] › feedback for affinity publisher v1 on desktop › change straight quotes to typographic quotes
Change straight quotes to typographic quotes - Feedback for Affinity Publisher V1 on Desktop - Affinity | Forum
June 5, 2025 - Is there a way to quickly change straight single and double quotes to typographical (curly) quotes in a selection of text? I know Publisher will currently change those things when typing, but it does not appear to do so when pasting from another source. So far, I can only think to run four regex ...
🌐
Reddit
reddit.com › r/phphelp › question: why do php developers hate sprintf()?
r/PHPhelp on Reddit: Question: Why do PHP developers hate sprintf()?
January 5, 2022 -

I just can't figure it out. My background is in other languages. I get that sprintf() has had some fun issues in C when people don't properly account for length. But in PHP I see code all the time that, instead of using a very clear and concise sprintf(), use concatenations that are impossible to understand easily. Where does this come from? I'm used to something along the lines of:

$s = sprintf("My %s has %d %s and that makes me %s",
    $spouse, $petNumber, $petType, ($goodEmotion ? "happy" : "sad"));

In PHP, I am constantly seeing this:

$s = "My ".$spouse." has $petNumber $petType and that makes me "
    .$goodEmotion ? "happy" : "sad";

I'm honestly curious where this comes from. Is there a history of issues with sprintf() in PHP or is there something else?

edit: updated in markdown to show code

🌐
Sublime Forum
forum.sublimetext.com › t › php-syntax-highlighting-between-html-quotes › 9233
PHP syntax highlighting between HTML quotes - Technical Support - Sublime Forum
March 8, 2013 - But i just can’t seem to find how it’s possible to get the right PHP syntax highlighting when working with a PHP / HTML combination. And that’s kind of important. One may use PHP code inside HTML quotes like in the screenshot, but the the PHP syntax isn’t right anymore. The screenshot ...
🌐
SitePoint
sitepoint.com › php
Applying the '\\' escape character to unescaped single quotes (') with preg_replace()? - PHP - SitePoint Forums | Web Development & Design Community
August 16, 2009 - I’d like to escape single quotes in a string with the backslash (\) character, but only escaping the single quotes that aren’t already escaped. To elaborate, the reason I’m doing this is so I can make the string safe for use with the eval() function, e.g. eval(‘$new = \’‘.$string.’\‘;’); At first I just did $string = str_replace("'", "\\'", string ); But I ran into problems with strings where a single quote was already escaped.
🌐
Reddit
reddit.com › r/php › do you use contact, sprintf or in-string variable?
r/PHP on Reddit: Do you use Contact, Sprintf or In-String Variable?
October 12, 2018 - I've been using in-string a whole lot lately, especially since moving to PHPStorm. It syntax highlights mistakes pretty effectively so it's not all that hard to make sure I didn't screw up. It changes the color of the variables in the strings if I use double quotes, doesn't change the color if using single quotes, for example.