March 30, 201016 yr 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
March 30, 201016 yr 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.
March 30, 201016 yr 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.
March 30, 201016 yr 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
March 30, 201016 yr 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
March 30, 201016 yr 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
March 31, 201016 yr 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