0

I'm learning some php right now and I'm trying to identify a best practice for using quote marks. I've decided I'll be using single quotes and concatenate variables into strings when needed.

I've now come to adding CSS class/ids into my php. When adding a class or id tag in php it works with escaping single quotes inside my single quotes OR using non-escaped paired double quotes inside the single quotes. So which is more efficient?

Example:

echo '<span class=\'big\'>This is big text</span>';

or

echo '<span class="big">This is big text</span>';
David Gelhar
  • 27,873
  • 3
  • 67
  • 84
DS-Matt
  • 95
  • 4
  • 3
    I would bet the difference would be less than a micro-second. – Jonathan Kuhn May 15 '13 at 22:39
  • 1
    Flip a coin, pick whichever you like... – millimoose May 15 '13 at 22:44
  • Actually, using single quotes in HTML attributes is prohibited in some standards and it's de facto standard to use double quotes. If the question is not about single or double quotes in PHP but rather in HTML it's a no brainer: single quotes in PHP, double quotes in HTML attributes' values. – diego nunes May 16 '13 at 03:11
  • In this question, [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php), is a complete answer. Its worth to read. –  May 16 '13 at 03:13

5 Answers5

0

It is better to use double quotes, so second option is much better.

HTML coding standard says that double is good :)

See this article. Are single quotes allowed in HTML?

Community
  • 1
  • 1
Bandydan
  • 623
  • 1
  • 8
  • 24
  • 3
    Which part of that "coding standard" says one should be preferred over the other? – Eric May 15 '13 at 22:48
0

I am not going to say witch one is best, this is a personal choice but I would say that W3C uses for the HTML code " so your code needs to look like this:

<span class="class_name">Text here</span>

(I would go with single quote, this way you can write clean HTML code that will render as it should with "" browser)

Hope that helps.

  • I think it's best to try and follow the W3C recommendations. I'm going to follow your recommendation. – DS-Matt May 16 '13 at 06:05
0

When echoing HTML double quotes are the correct way to do it.

For other applications the difference is so negligible it's not worth considering. Efficiency in PHP comes from ensuring no memory leaks are present and using good logic when creating complicated loops (exiting, returning, breaking etc so that it doesn't have to compute more than necessary).

Obviously there are cases when escaping is required, but generally I just go with your second option because that's the way I've always done it.

James
  • 3,233
  • 3
  • 40
  • 57
0

Really personal and self understanding,as language concern for PHP

  • php treat single quote without much consideration, hence less load
  • php treat double quote , parse it, search for variable name in scope, little more load.
  • single inside double, php pause variable look up as single quote comes, little less saving
  • double inside single , look up temporary , little more saving

Now it is up to you what you apply, once again pure objective answer

Notepad
  • 1,659
  • 1
  • 12
  • 14
0

The main different between single and double quote, is that variables in double quote will parse by PHP:

$name = 'World';
echo 'Hello $name'; // single quote
// OUTPUT: Hello $name

$name = 'World';
echo "Hello $name"; // double quote
// OUTPUT: Hello World