Social Icons

Tuesday, June 4, 2013

Difference between single quote and double quote in PHP

In PHP, if yu have to display or echo something (say a string), then you can do it with the help of single quote and double quote both. But there is a little difference between them as :

A single quoted string doesn't have variables within it interpreted but a double quoted string does. It can be better understood with the following example:

<?php
$a = 'prem';
echo 'My name is $a';
echo "My name is $a";
?>

For the first echo statement having single quote, output will be
My name is $a

For the second echo statement having double quoted string, output will be
My name is prem

So, if you have to define a constant string then better use with single quote and if you have variable too in the string then go with double quotes.

No comments:

Post a Comment

Total Pageviews