NYCPHP Meetup

NYPHP.org

[nycphp-talk] Variables syntax

Dan Cech dcech at phpwerx.net
Fri Oct 28 17:12:58 EDT 2005


I tend to go with version D:

$print_first_name = 'You said your first_name is '. 
$_SESSION['first_name'] .'.';

As you can see I use single-quotes around the 'string' portions and I 
have modified the spacing rule a little so that I don't leave a space 
between the 'string' portions and the . concatenation operator.

There are a few reasons I use this approach, but chief among them is 
that it makes child's play of spotting variables, especially in a 
syntax-highlighting editor.  If they're embedded within a string they're 
much harder to spot (for me at least).

In the end it comes down to personal preference, my testing indicates 
that the performance difference between single-quotes, double-quotes or 
double-quotes with complex (curly) braces is not large enough to really 
be worth considering unless you are talking about a LOT (> 100,000) 
embedded variables.  I would suggest  going with whatever makes your 
code the most consistent and easiest to maintain.

Dan

Aaron Fischer wrote:
> 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']}.";
> 
> At one time I think I had something like:
> Version B:
> $print_first_name = "You said your first_name is $_SESSION[first_name].";
> 
> 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?
> 
> 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. 
> 
> Basically I'd like to establish a standard for myself and want to make 
> sure I'm understanding the usage and scenario's which can arise.
> 
> Is there a better alternative method?
> 
> Comments, thoughts, suggestions?
> 
> Thanks!
> 
> -Aaron



More information about the talk mailing list