NYCPHP Meetup

NYPHP.org

[nycphp-talk] single quote vs. double quote

Kenneth Dombrowski kenneth at ylayali.net
Wed Apr 4 15:46:35 EDT 2007


On 07-04-04 09:29 +0530, Anirudh Zala wrote:
> On Tuesday 03 April 2007 19:00, tedd wrote:
> > At 8:51 AM -0400 4/3/07, Ken Robinson wrote:
> > >At 08:43 AM 4/3/2007, tedd wrote:
> > >>I'm not sure if what you are saying includes this, but I use double
> > >>quotes all the time in php for producing html. For example:
> > >>
> > >>[1] echo("$myResult <br/>");
> > >>[2] echo('<a href="mydomain.com/mywidget.php" >$myResult</a>');
> > >>
> > >>The use of double quotes in [1] allows me to print something
> > >>without having to use the dot operator.
> 
> It is matter of preference and convenience. As I said when you use double 
> quotes to enclose expression, PHP will try to look for "constants" that will 
> match part of static string. If constant is not found then will use string as 
> it is but if found then will replace that part of string by matching 
> constant's value.
> 
> In your above example, if there is defined a constant as "href" (though not 
> likely to exist) then it's value will be replaced in final output. That is 
> why "" should not be used there. 

This is incorrect.  Strings are never evaluated for constants.
Double-quoted strings are evaluated for "$variableExpansion" , which
incurs a slight processing cost 

Unquoted strings are first evaluated as constants, and if not found, an
E_NOTICE is issued, and the unquoted string is treated as a string
literal (as if it were surrounded by single quotes) 

kenneth at gilgamesh:/tmp$ php -r 'define("XYZ", "hi there"); echo "XYZ\n"; echo XYZ . "\n";'
XYZ
hi there

except for that detail, I agree with Anirudh's advice to not use "" except 
for the few places you need it ("\n", etc) 

Kenneth 




More information about the talk mailing list