NYCPHP Meetup

NYPHP.org

[nycphp-talk] single quote vs. double quote

Anirudh Zala arzala at gmail.com
Tue Apr 3 23:59:37 EDT 2007


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.
> >
> >How you write number [1] comes down to personal preference. I'd
> >rather write it as:
> >
> >echo $myResult . '<br/>';
> >
> >In number [2], I hope you realize that the string '$myResult' will
> >be treated as a static string and will not be evaluated.
> >
> >Also, since "echo" is a language construct and not a function, the
> >parenthesis are not required.
>
> 1. Yeah, you're right. In that example I have to put in those escape
> operators to get it to work correctly. But in my defense, I was
> typing code on the fly -- it was the topic and not the syntax I was
> addressing.
>
> echo("<a href=\"mydomain.com/mywidget.php\" >$myResult</a>");

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. Hence proper expression could be written 
like below:

echo '<a href="mydomain.com/mywidget.php">'.$myResult.'</a>';

Now there is no harm of expansion of static data.

>
> 2. Yes, echo is language construct and not a function, but my
> personal preference is to use the parentheses.  It makes it easier
> for me to read and I know if I want to send it more than one
> parameter, then I can't use parentheses. But, my simplistic approach
> to problems usually finds another way.
>
> Cheers,
>
> tedd

Anirudh Zala



More information about the talk mailing list