Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Best Practice with HTML and PHP quote marks?

Featured Replies

Hi guys,

 

Is there an accepted best practice for what quote marks to use and when in PHP and HTML development?

 

From what I can tell from looking around (and reading code snippets), it is generally accepted to use single quote marks in PHP unless you are outputting HTML, where you use double. This avoids the necessity of escaping quote marks for output.

 

And therefore, HTML markup should be double quoted.

 

Is this about right?

 

Thanks!

 

Martin

The differences are defined here (I think):-

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

 

This example

echo 'Arnold once said: "I\'ll be back"';
// Outputs: Arnold once said: "I'll be back"

 

where the echo statement has single quotes because the text inside has a double quotes which would close the echo statement if it also used double quotes, so the single quotes avoid an issue with the double quotes in the text, but the single quote in the text would close the echo statement's single quotes, so the single quote in the text has to have a backslash to escape it.

basic rule:

If you use double quoted strings in PHP, it will check the string inside to see if any of the elements are variables. If you have a string 100s of words long, that's going to be a lot of variable in memory. So basically, with regards to PHP, avoid double quotes unless you need to parse a variable inside a string. Better still, just break out of the string and concatenate the variable.

personally I use double quotes in two instances. When writing query strings for mysql, and when making new line characters or tabs in text directly (such as "\r\n\t"). I would say the complete opposite for outputting html. Say you are writing an <a> tag, you would need to escape all the quotes for elements such as href, class, id and so on

'<a id="mylink" href="http://www.example.com/" class="someclass">Text here</a>'

That compared with the double quote equivalent

"<a id=\"mylink\" href=\"http://www.example.com/\" class=\"someclass\">Text here</a>"

which makes me cringe every time I see it. people always say that it's acceptable to use double quotes when doing stuff like

"Hello, your name is $name" which I think is bad too since you are better off using sprintf and substituting the values in that way

  • Author

Thanks guys!

 

Jay - I don't think I wrote the HTML bit very clearly... Sorry. I meant that you would use 'single quotes' for the PHP but the output you want printed would be "double quoted" - exactly as you wrote in your example.

 

Just a quick question on the "Hello, your name is $name" example... I would always write that as:

 

'Hello, your name is '.$name.''

 

Is that not the best way of doing it, for consistancy?

 

Cheers!

 

Martin

Hi Martin. That method is fine, although personally I use the

sprintf('Hello, your name is %s', $name)

 

or the advanced version of sprintf inserts like

sprintf('Hello, your name is %1$s', $name)

 

Of course with strings as small as those, its not really worth bothering, but with longer strings and mulitple inserts it becomes far tidier IMO and helps you maintain consistency.

 

Also, with your version above, the .'' is pointless

Sorry. I meant that you would use 'single quotes' for the PHP but the output you want printed would be "double quoted" - exactly as you wrote in your example.

 

which would be

echo '"Hello, your name is '.$name.'"';

 

single quotes outside double quotes around text and $name concatenated and not included in single quotes.

 

Output "Hello, your name is John"

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.