SitePoint Sponsor |
|
User Tag List
View Poll Results: Single quotes or double quotes?
- Voters
- 90. You may not vote on this poll
Results 1 to 25 of 72
Thread: Quoting strings: what do YOU do?
Hybrid View
-
Sep 29, 2008, 01:16 #1
- Join Date
- Aug 2008
- Location
- The Netherlands
- Posts
- 9,097
- Mentioned
- 153 Post(s)
- Tagged
- 3 Thread(s)
Quoting strings: what do YOU do?
As you all probably know, there are different types of string quoting in PHP.
Let's start with single quote vs double quote:
PHP Code:$bar = "foo";
print "hello $bar"; // prints "hello foo"
print 'hello $bar'; // prints "hello $bar"
PHP Code:print "test"; // prints "test"
print 'test'; // also prints "test"
print test; // assuming 'test' is not defined as a constant, also prints "test"
is definitely the worst, because the compiler will first have to check if 'test' is a defined constant, and only after it figures out this isn't the case, will it print the string "test".
The first example (print "test"also isn't great, because the interpreter has to scan the whole string for occurrences of variables. For this short string this isn't any problem, but for larger strings it might become noticeable.
For these reason I personally ALWAYS stick to single quotes.
What do you use?Rémon - Hosting Advisor
SitePoint forums will switch to Discourse soon! Make sure you're ready for it!
Minimal Bookmarks Tree
My Google Chrome extension: browsing bookmarks made easy
-
Sep 29, 2008, 02:34 #2
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
That's an interesting question, heres what I found.
When starting out everything was in a print statement.
$name = "Earl"
print("My name is $name");
I mean every single line of HTML. It seemed flawless because it was a discipline I could remember, it was simple and it worked. I rarely come across remaining old code, but sometimes I do, and I groan. Still, not having to debug my print statements left me free to debug everything else I was doing wrong.
Slowly I started to use echo and single concatenated quotes, mostly after reading posts on this subject on this forum.
echo 'My name is ' . $name ;
I found this made my vars easier to spot, and was much influenced by the fact I was learning OOP - which generally speaking, should return data, not echo or print output directly.
I went down the road of comma separated echos for a while:
echo 'My name is ', $name ;
Which is even faster ... wheeee ....
But, I found it harder to be consistent, so I stopped using that.
Then of course Heredoc came out, so monster blocks of code including Javascript (wow!) could finally be injected into your page as is.
So given that I am now layering my applications, generally speaking, not outputting much html directly, I allow myself the freedom to use both single and double quotes as and when the situation suits me. for example a pile of vars to go in a string:
echo "The $size $feline $posture on the $product." ;
( the fat cat sat on the mat)
rather than
echo 'My name is ' . $name ;
Maybe my "readability imperative" is now to do with the flow of data rather than the html.
Choosing single or double quotes is often governed by where its headed, if its back into an Ajax-driven DOM then its often singles inside doubles to minimise all the inevitable backslashing crap.
So when starting out pick one method and stick to it for a while. Train your editor to create the text with a shortcut key.
But when you gain confidence and knowledge, experiment, and don't be afraid to use both/ether.
Eventually this becomes a non-issue as you tend to output very little to the the page, or that output goes into some kind of placeholder, such as a template.Last edited by Cups; Sep 29, 2008 at 02:36. Reason: consistent not read ....
-
Sep 29, 2008, 02:38 #3
- Join Date
- Aug 2008
- Location
- The Netherlands
- Posts
- 9,097
- Mentioned
- 153 Post(s)
- Tagged
- 3 Thread(s)
Thanks for your reply Cups, very nice explanation ! I appreciate it
Rémon - Hosting Advisor
SitePoint forums will switch to Discourse soon! Make sure you're ready for it!
Minimal Bookmarks Tree
My Google Chrome extension: browsing bookmarks made easy
-
Sep 29, 2008, 02:39 #4
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
I have seem to fallen in love with sprintf as of late, I find it a much cleaner way of concatenating and outputting variables encoded within strings.
Does anyone else do this, or is this just a phase I'm going through?
PHP Code:<?php
$sString = 'foo';
echo $sString;
echo sprintf('Hello %s',$sString);
?>
-
Sep 29, 2008, 08:17 #5
- Join Date
- Jul 2003
- Location
- Northeastern USA
- Posts
- 4,617
- Mentioned
- 56 Post(s)
- Tagged
- 1 Thread(s)
Code PHP:$foo="bar"; echo "My name is ".$foo;
I always use double quotes and always refer to the variable outside the quotes. As far as I know, it's the same basic fail-safe method across PHP, Java, JavaScript, and C#
-
Sep 29, 2008, 17:25 #6
- Join Date
- Mar 2008
- Location
- NP, New Zealand
- Posts
- 576
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Sep 30, 2008, 09:01 #7
- Join Date
- Sep 2006
- Location
- Nottingham, UK
- Posts
- 3,133
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Likewise. I do tend to switch between double and single - I use single for 'simple' text things like constant names in define() etc. Mostly double though, and I never embed variables within it. I don't use heredoc either - I will switch out of PHP instead.
Anyone using efficiency as a reason is doing it wrong! Readability and maintainability is FAR more important than efficiency/speed. The differences are absolutely minute, and you shouldn't code for efficiency except in the most extreme examples, you will end up making your code look awful / nightmare to maintain.
Despite several attempts at trying to work it out, I never ever saw the point of sprintf() or why you should use it.
-
Sep 30, 2008, 13:59 #8
It is good for ambiguous data into a certain format. For example finding all URLs in a string turning them into html anchors.
PHP Code:<?php
define( 'ANCHOR_FORMAT', '<a href="%s">%s</a>' );
function convertUrls ( $m )
{
$href = $m[1]; $title = $m[1];
# Do something with $href and $title
return sprintf( ANCHOR_FORMAT, $href, $title );
}
$s = preg_replace_callback( '/.../', 'convertUrls', $s );
-
Sep 29, 2008, 09:55 #9
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Does anyone else do this, or is this just a phase I'm going through?
HereDoc makes life much, much easier.
PHP Code:echo <<<FORM
<form action="{$_SERVER['PHP_SELF']}" method="post" onsubmit="alert('I can use both double and single quotes without needing to escape them. Not a care in the world.');" />
<select name="mySelect">
{$optionList}
</select>
<input type="submit" value="Go go go!" />
FORM;
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 29, 2008, 11:35 #10
- Join Date
- Oct 2006
- Location
- Kathmandu, Nepal
- Posts
- 4,013
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I use Single quotes and of course the variables outside of the quotes but until I go for HeareDoc or NewDoc. Single quotes doesn't take compiler to take care of variable interpolation as far as I know.
Mistakes are proof that you are trying.....
------------------------------------------------------------------------
PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5
-
Sep 29, 2008, 12:49 #11
The only problem with HereDoc is it doesn't look very nice with indented code.
-
Sep 29, 2008, 13:15 #12
- Join Date
- Dec 2005
- Posts
- 982
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:$percent = 99.9; echo 'I use single quotes ' . $percent . '% of the time';
Code:?> <form action="<?php echo($form['action']); ?>"> ... </form> <?php
MySQL v5.1.58
PHP v5.3.6
-
Oct 2, 2008, 12:31 #13
- Join Date
- Aug 2006
- Location
- Chicago
- Posts
- 542
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I used to escape the PHP code to write large blocks of HTML, but I found it more messy when I started using frameworks. This was the point when I changed the way I integrated PHP into the website.
Now I try to keep the views and logic as separate as possible. All the HTML is on separate php files, and PHP is only present there as presentation logic. (like displaying content through a loop)
-
Sep 29, 2008, 13:31 #14
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
The only problem with HereDoc is it doesn't look very nice with indented code.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 29, 2008, 13:37 #15
- Join Date
- Aug 2008
- Location
- The Netherlands
- Posts
- 9,097
- Mentioned
- 153 Post(s)
- Tagged
- 3 Thread(s)
Rémon - Hosting Advisor
SitePoint forums will switch to Discourse soon! Make sure you're ready for it!
Minimal Bookmarks Tree
My Google Chrome extension: browsing bookmarks made easy
-
Sep 29, 2008, 13:30 #16
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
The only problem with HereDoc is it doesn't look very nice with indented code.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 29, 2008, 14:03 #17
Simple simple...
PHP Code:<?php
class NothingSpecial
{
public function nothingHere ()
{
$var = <<<EOLINE
...stuff...
...stuff...
...stuff...
...stuff...
EOLINE;
return $var;
}
}
-
Sep 29, 2008, 14:26 #18
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
It's just one line that is out if you tab the rest:
PHP Code:<?php
class NothingSpecial
{
public function nothingHere ()
{
$var = <<<EOLINE
...stuff...
...stuff...
...stuff...
...stuff...
EOLINE;
return $var;
}
}Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 29, 2008, 15:23 #19
-
Sep 29, 2008, 14:39 #20
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
Yeah, same "problem" with vim - it frigs with the code-folding, well, sometimes.
Heredoc states the closing tag must be the first char on a line.
$thing = <<<EOL
Do stuff.
EOL;
In vim, this occasionally causes the code-folder to throw a wobbler, its ok if I close and reopen the file. It doesn't stop me doing using it - its just a PITA.
I had to refactor some stuff this pm, and now I look at it I am amazed how much I have unconciously adopted the method that BrandonK posted about, I just drop in and out of PHP.
Read about the alternative PHP syntax too.
-
Sep 29, 2008, 17:17 #21
- Join Date
- Mar 2008
- Posts
- 1,149
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I usually put my HTML in separate template files. The PHP files are cleaner, easier to read, easier to scan through, easier to modify later, and easier to write.
Although when I do need to use variables in strings, I use whatever is appropriate for the project.
-
Sep 30, 2008, 07:00 #22
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
I always use double quotes and always refer to the variable outside the quotes. As far as I know, it's the same basic fail-safe method across PHP, Java, JavaScript, and C#
Does anyone know offhand how quoting is done in Python? (too busy/lazy to look, atm)
-
Sep 30, 2008, 08:50 #23
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
In Python, the basic concatenation operator is +. Both quote types can be used (unlike C# and Java) and also, Print has indefinite params so for speed you could use:
Code python:name = 'Rob' + 'ert' print 'Hello ', name, '!'
It also has a similar way of formatting like PHP's sprintf(), which looks like:
Code python:print 'Hello %s!' % (name)
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 30, 2008, 14:02 #24
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
Does anyone know offhand how quoting is done in Python? (too busy/lazy to look, atm)
-
Sep 30, 2008, 14:32 #25
I prefer single quotes myself, always put my variables outside quotes so singles are faster
Bookmarks