Re: Back to Basics - Why Use Single Quotes?

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

 



Thanks for the nice explanation, Dan!

On 7/30/08, Daniel Brown <parasane@xxxxxxxxx> wrote:
>
> On Wed, Jul 30, 2008 at 6:51 PM, Stephen <stephen-d@xxxxxxxxxx> wrote:
> > I have traditionally used double quotes going back to my PASCAL days.
> >
> > I see many PHP examples using single quotes, and I began to adopt that
> > convention.
> >
> > Even updating existing code.
> >
> > And I broke some stuff that was doing variable expansion. So I am back to
> > using double quotes.
> >
> > But I wonder, is there any reason to use single quotes?
>
>    Single quotes means literal, whereas double quotes means translated.
>
>    For example:
>
> <?php
>
> // This returns exactly the same data:
> $foo_a = "bar";
> $foo_b = 'bar';
>
> echo $foo_a; // bar
> echo $foo_b; // bar
>
>
> // This returns different data:
> $foo = "bar"; // Single quotes can be used here just the same.
>
> echo "The answer is $foo"; // The answer is bar
> echo 'The answer is $foo'; // The answer is $foo
>
>
> /* And if you want to use special
>    characters like newlines, you
>    MUST use double quotes. */
>
> echo "This echoes a newline.\n"; // This echoes a newline. [newline]
> echo 'This echoes a literal \n'; // This echoes a literal \n
>
> ?>
>
>    Basically, double quotes evaluate certain things and return the
> evaluation, while single quotes return EXACTLY what's typed between
> them.
>
> --
> </Daniel P. Brown>
> Better prices on dedicated servers:
> Intel 2.4GHz/60GB/512MB/2TB $49.99/mo.
> Intel 3.06GHz/80GB/1GB/2TB $59.99/mo.
> Dedicated servers, VPS, and hosting from $2.50/mo.
>
> --
> 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