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]

<?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

?>

[/php]

Leave a Reply