This post is part of the PHP Snippets series where I will be covering the basics of developing in PHP.
Both single and double quotes are supported in PHP, but there is a significant difference in how they are handled.
Single quotes are output “as is” and are the quickest form of quoting. When using single quotes, you would need to concatenate variables with a string to output them:
$fruit = 'apples';
echo 'They drank some juice from ' . $fruit;
Double quotes evaluate variables and many escaped characters. This means you do not need to concatenate variables with the string, but can include them inside the double quoted string:
$fruit = 'apples';
echo "They drank some juice from $fruit";
You can also use curly braces ({}
to isolate the variable within the string. When PHP evaluates a string and sees a $
it will take as much of the string as part of the variable name (ending at a space or punctuation), but you can use the curly braces to explicitly set the end of the variable name:
$fruit = 'apple';
echo "They drank some juice from ${fruit}s.";