PHP strings can be specified not just in two ways, but in four ways.
- 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). - 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
$typeand you want toecho "The $types are". That will look for the variable$types. To get around this useecho "The {$type}s are". Take a look at string parsing to see how to use array variables and such. - 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. - 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 OverflowHow to include single quotes in the value of a text string - PHP - SitePoint Forums | Web Development & Design Community
PHP double quote/single quote/string/array interaction
Question: Why do PHP developers hate sprintf()?
PHP syntax highlighting between HTML quotes
PHP strings can be specified not just in two ways, but in four ways.
- 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). - 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
$typeand you want toecho "The $types are". That will look for the variable$types. To get around this useecho "The {$type}s are". Take a look at string parsing to see how to use array variables and such. - 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. - 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.
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.
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?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