Monday 11 June 2012

Difference between double quotes and single quotes in PHP



In PHP string data is enclosed in quotes either single or double quotes. But sometimes programmers get confused in difference between single quote and double quote.
Let me tell you what is main difference between single quotes and double quotes theoretically & also practically too.
Single quotes do not support variable expansion but double quotessupport.Means if there is a string with single quotes and if you place a variable in that, it won’t substitute its value in string.
e.g.
<?PHP
         $str = 'QAPHP';
         echo '$str is a Blog which provides Question & Answers.';
?>

Output will be:: $str is a site which provides Question & Answers.
In case of double quoted string…

<?PHP
         $str = 'QAPHP';
         echo "$str is a site which provides tips.";
?>

Output will be:: QAPHP is a Blog which provides Question & Answers.

Means if there is a string with double quotes and if you place a variable in that, it will substitute its value in string.
This is the main difference.
Reference : http://www.scriptvenue.com/2011/10/difference-between-double-quotes-and-single-quotes-in-php/