6

Possible Duplicate:
Difference between single quote and double quote string in php

Hi,

What is the difference between echo 'Test Data'; and echo "Test Data"; in PHP.

Both statements give me same output.

jww
  • 97,681
  • 90
  • 411
  • 885
Pradip
  • 1,317
  • 4
  • 21
  • 36

2 Answers2

14

I believe that double quotes allow variables to be replaced by the value :

echo "test = $test";

displays :

test = 2

echo 'test = $test';

displays :

test = $test

Sebastian Hoitz
  • 9,343
  • 13
  • 61
  • 77
Réjôme
  • 1,474
  • 3
  • 16
  • 25
1

Single-quoted strings will not have variables or escape sequences expanded by the interpreter, whereas double-quoted strings will - look at the different output of:

$foo = 'bar';
echo 'This is a $foo';
echo "This is a $foo";

Single-quoted strings are hence marginally 'better' to use as the interpreter won't have to check the contents of the string for a variable reference.

Gavin Ballard
  • 1,251
  • 2
  • 9
  • 14
  • Double or single quotes make absolutely no difference for speed at all. See my personal test @ http://gnur.nl/speed.php – gnur Mar 11 '11 at 13:44
  • @gnur: After having a quick look at a few benchmarks, you're probably right that the performance difference is negligible. The deciding factor should be readability and ease of code (which I think tends to side with `"This is a $foo."` over `'This is a ' . $foo . '.'`.) – Gavin Ballard Mar 23 '11 at 09:04
  • I agree for readability, but when you use more languages it makes more sense to not include variables in your strings because they aren't supported most of the them. – gnur Mar 23 '11 at 10:40