哪种方法更有效,在单引号内使用双引号或转义单引号? [关闭]

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>';

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.

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?

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.

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

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