search
top

PHP treats single quotes and double quotes differently

When working with strings, it is important to understand the difference in how PHP treats single quotes (echo ‘Hello $name’; ) as compared with double quotes (echo “Hello $name”; )

Single quoted strings will display things exactly “as is.” Variables will not be substituted for their values. The first example above (echo ‘Hello $name’; ) will print out Hello $name.

Double quote strings will display a host of escaped characters and variables in the strings will be substituted for their values. The second example above (echo “Hello $name” ) will print out Hello Alan if the $name variable contains ‘Alan’.


<?php

echo 'this is a string';

echo “this is a string”;

//both output the same thing

$icecream =”chocolate”;

echo 'I like $icecream icecream';  // prints I like $icecream ice cream

echo “I like $icecream icecream”;  // prints I like chocolate ice cream

?>

One Response to “PHP treats single quotes and double quotes differently”

  1. It seems like you understand quite a lot
    regarding this particular topic and it all shows
    thru this specific blog post, labeled “PHP treats single quotes and double quotes differently
    | Joy of PHP Programming”. Thanks ,Cheryl

Leave a Reply

top