Re: When and when not to use ' and "

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, June 2, 2007 3:15 pm, Brian Seymour wrote:
> $bar = $foo['$colVal']; // didn't work
> $bar = $foo['{$colVal}']; // didn't work
> $bar = $foo[$colVal]; // worked
> $bar = $foo['id']; // obviously worked
>
> What I don't understand is why the first or second option didn't work.
>
> Can anybody shed some light on this?

' is actually easier to explain.

' has only TWO special characters you can embed in it: ' and \

$foo = 'That\'s life!';
$bar = 'The backslash \\ should be escaped, because it\'s special';

You don't HAVE to escape \ with \\ if PHP won't get "confused" by
whatever else you have in your string, but it's a Good Idea, imho, to
just always escape it, so you understand what the string parser is
doing internally.

" is a bit more complex.

In addition to " and \ being special, exactly parallel to ' and \
inside '', you also have:
  variables like $foo and 1-D arrays like $foo[id]
  special control characters like \n \t \r
  any char you want with octal
  { and } for some versions (ugh) of PHP

There is also heredoc syntax, which is probably best for large chunks
of text, in general.

Full documentation is here:
http://us.php.net/manual/en/language.types.string.php

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux