Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP single and double quotes


ghost's Avatar
0 0

What's the deal with single and double quotes in PHP? I usually use only double quotes, but my question is what circumstances would each be used? Or are they used interchangeably?


ghost's Avatar
0 0

Ah I found the answer. Basically, variables inside of double quotes will be parsed whereas single quotes would not. Example:

$name = "Bob";
echo("Hello $name");
?>```
 
The result there would be Hello Bob, whereas in the single quotes:
 
```markup<?
$name = "Bob";
echo('Hello $name');
?>```

 
The output would be Hello $name. This really was never relevant to me, because I always used the dots echo("Hello" . $name . "!"); but I guess this could save me a bit of time. 

ghost's Avatar
0 0

Use double-quotes if you want it to automatically parse variables, if you use single-quotes you have to separate the variables and text with "."s. But I think I heard somewhere that single-quotes are processed faster, because php doesn't have to parse them. I know that a lot of people say that when outputting text like "hello world" that contains no variables, then single-quotes are marginally faster.


ghost's Avatar
0 0

single quotes have text as is, double quotes will do extra processing.

for example, escape characters \n \t etc, will only work in double quotes.

i suppose if you are tryin to boost efficencey, use single, if you need the extra processing use double., tho the diff isnt really that much