Difference Between " and ' on PHP - Save My Knowledge
Wednesday, September 17, 2014

Difference Between " and ' on PHP

Hi, maybe some of you wondering, what is the difference between " and '. It both can be used to store string into variable or you can use it for echoing or print your string. But what is the difference? Some time ago, I got error while echoing '$name', it didn't show up as I expected (as example, the name is "johana"), but it show up as $name on browser.

But if you echo the $name using ", you will get your expected result = johana. From this case, we can know that if " can process the inside of variable but ' will always show what it contains.

Let me give you an example:

$name = "johana";
$address = "walking street";

echo "$name have house at $address"; //This code will give you result "johana have house at walking street"
echo '$name have house at $address'; //This code will give you result "$name have house at $address"

So, what if I want to posses the content of the variable but using ' only?
The answer is simple, but it will make you work extra for code. You can do it like this

echo $name.' have house at '.$address; //This code will give you result "johana have house at walking street" as expected.
You just need to separate the variable with the string that you need to show up.

For me, I prefer to use ' because I can easily find out what is variable, or what is string that need to be printed on browser.