[nycphp-talk] Variables syntax
Hans Zaunere
lists at zaunere.com
Sun Oct 30 15:37:20 EST 2005
Aaron Fischer wrote on Friday, October 28, 2005 4:10 PM:
> Greetings listers,
>
> A basic question I'm sure... (I have checked PHundamentals and did
> some googling and manual searching and book skimming but haven't come
> up with the answers I'm looking for.)
>
> I have currently been setting variable like so:
> Version A:
> $print_first_name = "You said your first name is
> {$_SESSION['first_name']}.";
This is my standard...
> At one time I think I had something like:
> Version B:
> $print_first_name = "You said your first_name is
> $_SESSION[first_name].";
Ewww....
> This would yield the same result on my server, but I think version B
> is incorrect and version A is correct. I am thinking that Version B
> is incorrect as it is missing the single quotes around the var name,
> even though it does render OK on my server, perhaps it would not in
> another environment?
Absolutely - it's bad and sloppy. Version B is creating notices (because
PHP first would try to interpret first_name as a constant) but you won't see
them unless you have E_ALL as your error reporting level. This is an
instance of PHP being "nice" by second guessing what the code actually
means.
> Then I also had something like:
> Version C:
> $print_first_name = "You said your first_name is " .
> $_SESSION['first_name'] . ".";
>
> Now I think Version C is technically correct, and the decision to use
> A or C is based on personal preference.
Agreed, it's a matter of preference. There was a time in PHP when using
double quotes resulted in extra overhead because of some bug. I don't
remember all the details, but as I remember this has been long corrected (I
believe by Andi).
Personally, I prefer Version A because it's easier to read. I've never been
a fan of breaking strings up into a lot of concatenations with the period.
---
Hans Zaunere / President / New York PHP
www.nyphp.org / www.nyphp.com
More information about the talk
mailing list