PHP Snippets: Quotes

PHPThis 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.";

What should we write about next?

If there is a topic which fits the typical ones of this site, which you would like to see me write about, please use the form, below, to submit your idea.

Your Name

Your Email

Suggested Topic

Suggestion Details

Leave a Reply

Your email address will not be published. Required fields are marked *