0

in HTML

we can

face='Tahoma'
face="Tahoma"

and in PHP

$name = "Junaid";
$name = 'Junaid';
$names = array('Junaid','Junaid','Junaid');
$names = array("Junaid","Junaid","Junaid");

now all these statements are correct with single or double quotes but

  • what difference does it make
  • which is the preferred method
  • what types of quotes to use where

which one of the following is correct

$link = "www.anyLINK.com"

echo "<a href=' ". $link ." '>Click Here</a>"

echo "<a href= ". $link ." >Click Here</a>"
animuson
  • 53,861
  • 28
  • 137
  • 147
Moon
  • 19,518
  • 56
  • 138
  • 200

3 Answers3

2

The difference between single and double quotes in PHP is that PHP will read variables inside of double quotes but not single. For example:

<?php
$variable = "test";

echo "Can you see this $variable"; // Can you see this test
echo 'Can you see this $variable'; // Can you see this $variable

?>

The single quote will be read literal, where was double will attempt to replace the $variable with it's value.

Optimization Differences
As pointed out in the comments below, single quotes tend to be faster than double. In a quick benchmark, double quotes with any $'s escaped is the fastest vs single and double with and without $variables in the string. See http://codexon.codepad.org/54L3miwN

Robert
  • 21,110
  • 9
  • 55
  • 65
  • Little typo the you have mismatching quotes there. – Iznogood Aug 17 '10 at 02:53
  • 3
    That is correct in PHP but I would add that some people claim that using single quote is faster in PHP because the engine doesn't need to parse the string for possible variables. As for html and javascript, it doesn't make any difference. – Gabriel Aug 17 '10 at 02:55
  • @Gabriel it doesn't make difference for PHP too. Engine still does need to parse the string for possible ending delimiter and escape character. So, we still have to parse it. – Your Common Sense Aug 17 '10 at 06:11
  • Robert, learn to test properly. Get your real code, make it with quotes of your choice and run **apache menchmark** to emulate a real case. Then replace quotes and run ab again. You can run these tests for ages but never find even a a microscopic difference. – Your Common Sense Aug 17 '10 at 06:15
1

See http://php.net/manual/en/language.types.string.php.

In particular, variables are expanded in double quotes:

$foo = 42;
print('foo is $foo'); // foo is $foo
print("foo is $foo"); // foo is 42
Ed Mazur
  • 3,042
  • 3
  • 21
  • 21
0

In HTML, it doesn't matter at all.

In PHP, it does. Using the single-quotes prevents variables from being interpreted; for instance, echo '$foo'; will print "$foo". Not the variable, just the characters. Also, you have to escape single-quotes within single-quotes, but not single-quotes within double-quotes, etc. I answered this question before here.

As for your second question, they're both wrong. It should be:

echo "<a href='". $link ."'>Click Here</a>"

or, better yet:

echo "<a href='$link'>Click Here</a>"

or, better still, a templating engine like Smarty TPL.

Community
  • 1
  • 1
Borealid
  • 95,191
  • 9
  • 106
  • 122
  • The line "Also, you have to escape single-quotes within single-quotes, but not single-quotes within double-quotes, etc. " is insightful Thanks for the answer. Not sure why somebody gave a negative for this. – sjsam Jan 08 '15 at 05:51