views:

89

answers:

5

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>"
+3  A: 

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
Little typo the you have mismatching quotes there.
Iznogood
Yeah I caught it :P
Robert
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
@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.
Col. Shrapnel
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.
Col. Shrapnel
A: 

There is no functional difference for HTML. Quoting the W3C on SGML and HMTL:

By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa.

...

In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). We recommend using quotation marks even when it is possible to eliminate them.

Regarding php note the difference that @Robert pointed out in the other answer. You may also want to check out the following Stack Overflow post:

Daniel Vassallo
A: 

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.

Borealid
+1  A: 

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
Edward Mazur