IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Learning PHP Part Deux: When to use (') and when to use (")
Hello again one and all. Thanks for your help on arrays -- I'm pretty confident I understand them now, though I'll have to reread the chapter to get the actual "nuts and bolts" of how they're used locked down.

But Jay said something as an aside in the arrays thread that threw me for a loop:

The only thing I really have to add has nothing to do with arrays. In PHP you should get in the habit of using ' rather then " unless you need double quotes. It takes a small but not totally ignorable amount of extra procssing power to handle double quotes, becaue there are more things PHP has to scan for in the string.


The book I'm reading (PHP 5 for Dummies, if you must know -- I know a lot of people hold the Dummies books in disdain but I've found them invaluable since DOS for Dummies back in the early 90s) tends to favor the double quote for everything, since you can place variables and special characters inside them and they get translated correctly. Although I suppose it's not much more difficult to work around that, so that:


echo "The time is now $time";


becomes


echo 'The time is now ',$time;


The problem, I suspect, is that the book is dealing with truly simple scripts because it's aimed at those of us who are just learning all this stuff, so that either double or single quotes are "OK" because the scripts are so short that the processing power used is negligible.

So what I'm wondering is: when are the times that you *need* double quotes? If, as Jay suggests, I should get used to using single quotes for just about everything, what situations do I need to be looking for when double quotes are the better choice?

Thanks all.
"We are all born originals -- why is it so many of us die copies?"
- Edward Young
New Keeping in mind that I don't know PHP at all...
To quote your example:
echo "The time is now $time";

becomes

echo 'The time is now ',$time;
What happens when your straight-up text becomes:

The time's now

Do you need to double-single-quote the apostrophe? A la:

echo 'The time''s now ',$time;

Do you want to have to deal with that instead of (potentially):

echo "The time's now $time";

Again, I don't know PHP, so I'm not even sure this is an issue. Just pointing out something I thought of off the top of my head.
-YendorMike

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety."
- Benjamin Franklin, 1759 Historical Review of Pennsylvania
New Double quotes force evaluation of the contents
When you use double quotes, the parser has to read everything inside and decide if it's a variable that needs to be expanded. If you use single quotes, it just looks for the next single quote.

To get single quotes into a single-quoted string, you double it:
echo 'That''s the way.';


To get double quotes into a double-quoted string you escape it:
echo "He said, \\"Do this.\\"";


Reverse it and you get:
echo "That's the way.";
echo 'He said, "Do this."';


So there are lots of ways to do it. As a style issue, I prefer to only use double quotes when outputting text to the browser and dropping in values as I go. Within array keys, I always use single-quotes.

Poorly-documented gotcha. What's the difference?
echo "This belongs to $user["name"].";
echo "This belongs to $user['name'].";
echo "This belongs to $user[name].";

echo "This belongs to ".$user["name"].".";
echo "This belongs to ".$user['name'].".";
echo "This belongs to ".$user[name].".";

echo 'This belongs to '.$user["name"].'.';
echo 'This belongs to '.$user['name'].'.';
echo 'This belongs to '.$user[name].'.';


If I remember correctly, the first two don't work, the third one does. The last six all work, but the second-to-last is the way I'd do it. It's unambiguous and easy to read.


Oh, and while I can't remember the exact syntactic difference, I know I prefer a period instead of a comma in echo 'The time is now ',$time;
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New I thought the period was used to...
Oh, and while I can't remember the exact syntactic difference, I know I prefer a period instead of a comma in

echo 'The time is now ',$time;



I thought the period was used to append information to something in the previous line of a script?

For example:


echo 'Hello';
echo .' World';


Gave you "Hello World" -- both appearing on one line.
"We are all born originals -- why is it so many of us die copies?"
- Edward Young
New Err, not really
I thought the period was used to append information to something in the previous line of a script?

For example:


echo 'Hello';
echo .' World';


Gave you "Hello World" -- both appearing on one line.

If you run that it will print 'Hello World' on one line, but it has nothing to do with the period. PHP don't automatically include any line breaks, so everything goes on one line unless you include some yourself. This can be confusing, because if the output is HTML, it will often be rendered on multiple lines even if the output is on one.

Doing
echo 'This is one <br /> line of output';
will produce one line of output but render as two lines of HTML. But doing
echo "This is one line /n";
echo "and this is another one";
will output two lines and render as one in HTML.

The period is string concatenation in PHP. $name = 'Jay' . ' ' . 'Mehaffey' sets $name to 'Jay Mehaffey'.

The echo command takes any number of values, seperated by commas, and outputs them.

Thus echo 'The time is now ',$time; and echo 'The time is now' . $time are actually doing two subtly different things. The first outputs 'The time is now' and then outputs $time right after it. The second puts 'The time is now' and $time together into one string and then outputs that.

Jay
New Well they are never required
AFAIK, there is nothing in PHP that really requires double quotes, and I never use them when writing my own code. But it is mostly a matter of personal preference.

The thing to avoid is using double quotes on a static string. Don't write "Name: " . $name because your wasting processing power. Either do it "Name: $name" or 'name: ' . $name. Either way is acceptable, but you should pick one and use it consistantly.

It seems silly, but I've run across quite a bit of PHP code that mixes it. I have often see "Name: " . $name . "/n" in code*. My habit is define "/n" as a global constant and write 'Name: ' . $name . LF.

Jay

* Or even uglier stuff. 'The name ' . "is $name /n" and 'Name: ' . "$name" . "/n" are both terribly akward.


New On quoting array keys
Make sure you look at the [link|http://us2.php.net/manual/en/language.types.array.php|Array do's and don'ts] section. It covers single- double- and non-quoted array keys, as well as the effect of curly braces on variable expansion within double-quoted strings.

The valuable lesson to be learned from this section is that there are several constructs which may be logically equivalent to a parser, but they are not all equally human readable. I count readability as much more important than unexpected validity. That's why I generally don't put arrays into double quotes and let the parser expand them. It's too confusing to read.

echo 'I prefer to do '.$array['this'].'.';
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
Expand Edited by drewk Aug. 8, 2006, 02:37:31 PM EDT
New I have to say...
... I personally didn't find your example more "human readible." I had to spend five seconds figuring out what parts of that line were enclosed in quotes, and what parts weren't...
"We are all born originals -- why is it so many of us die copies?"
- Edward Young
New It looks better in vim
What, you don't use vim?
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Kate or Quanta...
or, for fast edits, nano.
"We are all born originals -- why is it so many of us die copies?"
- Edward Young
New I can't write code without syntax highlighting
Well, I can, but it's painful. The human brain just isn't wired for identifying matching braces and quotes. That's what computers are for.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New both Kate and Quanta have syntax highlighting
"We are all born originals -- why is it so many of us die copies?"
- Edward Young
New Sometimes it matters, sometimes it doesn't.
The key difference, as everyone's pointed out, is that "double-quoted strings" will expand $variables and control escapes (like \\n) whilst 'single-quoted strings' do neither. That means that '\\n' won't output a linefeed!

Being able to put variables instream in a string sounds like a nifty idea, after all, Perl works that way, but it makes the parser a bit hairy. Referencing arrays and objects in a string isn't as clean as outside a string. I avoid instream variables, myself, always preferring the concatenation operator.

I'd say us whichever you prefer, but as Jay has said, there is a cost to the instream expansion. I ran a simple test assigning the same string to another variables many thousand times and timed it. I did it one way using double-quotes and another way using single-quotes. The difference was negligable. Then I added something: I added the index counter to the string, so that PHP was doing either variable substitution in double-quotes or string concatenation with single. The latter was more than four (4) times faster. Yes, I was surprised, too.

Which brings me to '.': this character is the string-concatenation operator. It just joins two strings together. That's all it does.

Wade.
"Insert crowbar. Apply force."
     Learning PHP Part Deux: When to use (') and when to use (") - (cwbrenn) - (12)
         Keeping in mind that I don't know PHP at all... - (Yendor)
         Double quotes force evaluation of the contents - (drewk) - (2)
             I thought the period was used to... - (cwbrenn) - (1)
                 Err, not really - (JayMehaffey)
         Well they are never required - (JayMehaffey)
         On quoting array keys - (drewk) - (5)
             I have to say... - (cwbrenn) - (4)
                 It looks better in vim - (drewk) - (3)
                     Kate or Quanta... - (cwbrenn) - (2)
                         I can't write code without syntax highlighting - (drewk) - (1)
                             both Kate and Quanta have syntax highlighting -NT - (cwbrenn)
         Sometimes it matters, sometimes it doesn't. - (static)

Calendars are a minor issue.
104 ms