From: Ashley Sheridan on
On Tue, 2010-10-05 at 15:46 -0400, Steve Staples wrote:

> On Tue, 2010-10-05 at 20:35 +0100, Ashley Sheridan wrote:
> > On Tue, 2010-10-05 at 15:28 -0400, chris h wrote:
> >
> > > Benchmark and find out! :)
> > >
> > > What are you using this for? Unless you are doing something crazy it
> > > probably doesn't matter, and you should pick whichever you feel looks nicer
> > > / is easier to code in / etc.
> > >
> > > Chris H.
> > >
> > > On Tue, Oct 5, 2010 at 3:23 PM, saeed ahmed <saeed.sas(a)gmail.com> wrote:
> > >
> > > > $a = 'hey';
> > > > $b = 'done';
> > > >
> > > > $c = $a.$b;
> > > > $c = "$a$b";
> > > >
> > > > which one is faster for echo $c.
> > > >
> >
> >
> > As far as I'm aware, the first of the two will be faster, but only just.
> > As Saeed mentioned, the difference will be negligible, and unless you
> > plan to run a line like that in a loop or something hundreds of
> > thousands of times, you probably won't notice any difference.
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
>
>
> to be proper, shouldn't it technically be
> $c = "{$a}{$b}";
>
> ??
>
> Steve.
>
>


It doesn't have to use the braces. The braces only tell PHP exactly
where to stop parsing the current variable name. The following examples
wouldn't work without them:

$var = 'hello ';
$arr = array('msg 1'=>'hello','msg 2'=>'world');

echo "{$var}world";
echo "{$arr['msg 1']}{$arr['msg 2']}";

Without the braces, in the first example PHP would look for a variable
called $varworld, and in the second it would be looking for a simple
scaler called $arr, not the array value you wanted.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: chris h on
On Tue, Oct 5, 2010 at 3:53 PM, Ashley Sheridan <ash(a)ashleysheridan.co.uk>wrote:

> On Tue, 2010-10-05 at 15:46 -0400, Steve Staples wrote:
>
> > On Tue, 2010-10-05 at 20:35 +0100, Ashley Sheridan wrote:
> > > On Tue, 2010-10-05 at 15:28 -0400, chris h wrote:
> > >
> > > > Benchmark and find out! :)
> > > >
> > > > What are you using this for? Unless you are doing something crazy it
> > > > probably doesn't matter, and you should pick whichever you feel looks
> nicer
> > > > / is easier to code in / etc.
> > > >
> > > > Chris H.
> > > >
> > > > On Tue, Oct 5, 2010 at 3:23 PM, saeed ahmed <saeed.sas(a)gmail.com>
> wrote:
> > > >
> > > > > $a = 'hey';
> > > > > $b = 'done';
> > > > >
> > > > > $c = $a.$b;
> > > > > $c = "$a$b";
> > > > >
> > > > > which one is faster for echo $c.
> > > > >
> > >
> > >
> > > As far as I'm aware, the first of the two will be faster, but only
> just.
> > > As Saeed mentioned, the difference will be negligible, and unless you
> > > plan to run a line like that in a loop or something hundreds of
> > > thousands of times, you probably won't notice any difference.
> > > Thanks,
> > > Ash
> > > http://www.ashleysheridan.co.uk
> > >
> > >
> >
> >
> > to be proper, shouldn't it technically be
> > $c = "{$a}{$b}";
> >
> > ??
> >
> > Steve.
> >
> >
>
>
> It doesn't have to use the braces. The braces only tell PHP exactly
> where to stop parsing the current variable name. The following examples
> wouldn't work without them:
>
> $var = 'hello ';
> $arr = array('msg 1'=>'hello','msg 2'=>'world');
>
> echo "{$var}world";
> echo "{$arr['msg 1']}{$arr['msg 2']}";
>
> Without the braces, in the first example PHP would look for a variable
> called $varworld, and in the second it would be looking for a simple
> scaler called $arr, not the array value you wanted.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
Just to add in here, they are also required when calling an object's
properties, if - ready for this? - that object is itself a property of
another object.

so while this would work,

"$circle->circumference"

this would NOT work

"$circle->circumference->inches"

The later would be injecting the $circle->circumference property, followed
by string literal "->inches". So to get it to work you would need to use
curlys.

"{$circle->circumference->inches}"


Chris H.
From: TR Shaw on

On Oct 5, 2010, at 3:23 PM, saeed ahmed wrote:

> $a = 'hey';
> $b = 'done';
>
> $c = $a.$b;
> $c = "$a$b";
>
> which one is faster for echo $c.

Depends upon the platform its running on.


From: Steve Staples on
On Tue, 2010-10-05 at 20:53 +0100, Ashley Sheridan wrote:
> On Tue, 2010-10-05 at 15:46 -0400, Steve Staples wrote:
>
> > On Tue, 2010-10-05 at 20:35 +0100, Ashley Sheridan wrote:
> > > On Tue, 2010-10-05 at 15:28 -0400, chris h wrote:
> > >
> > > > Benchmark and find out! :)
> > > >
> > > > What are you using this for? Unless you are doing something crazy it
> > > > probably doesn't matter, and you should pick whichever you feel looks nicer
> > > > / is easier to code in / etc.
> > > >
> > > > Chris H.
> > > >
> > > > On Tue, Oct 5, 2010 at 3:23 PM, saeed ahmed <saeed.sas(a)gmail.com> wrote:
> > > >
> > > > > $a = 'hey';
> > > > > $b = 'done';
> > > > >
> > > > > $c = $a.$b;
> > > > > $c = "$a$b";
> > > > >
> > > > > which one is faster for echo $c.
> > > > >
> > >
> > >
> > > As far as I'm aware, the first of the two will be faster, but only just.
> > > As Saeed mentioned, the difference will be negligible, and unless you
> > > plan to run a line like that in a loop or something hundreds of
> > > thousands of times, you probably won't notice any difference.
> > > Thanks,
> > > Ash
> > > http://www.ashleysheridan.co.uk
> > >
> > >
> >
> >
> > to be proper, shouldn't it technically be
> > $c = "{$a}{$b}";
> >
> > ??
> >
> > Steve.
> >
> >
>
>
> It doesn't have to use the braces. The braces only tell PHP exactly
> where to stop parsing the current variable name. The following examples
> wouldn't work without them:
>
> $var = 'hello ';
> $arr = array('msg 1'=>'hello','msg 2'=>'world');
>
> echo "{$var}world";
> echo "{$arr['msg 1']}{$arr['msg 2']}";
>
> Without the braces, in the first example PHP would look for a variable
> called $varworld, and in the second it would be looking for a simple
> scaler called $arr, not the array value you wanted.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
Ash:

I understand what the {} does, but just like in HTML, it is more proper
to use lower case for the attributes/elements, and use " (double quotes)
when wrapping the attributes... but is it not "REQUIRED" to write it in
that manner... just like it is not required to wrap the variables in {}
when inside the ""...

that's just me, I tend to try and do that every time...

Steve.

From: Ashley Sheridan on
On Tue, 2010-10-05 at 15:58 -0400, Steve Staples wrote:

> On Tue, 2010-10-05 at 20:53 +0100, Ashley Sheridan wrote:
> > On Tue, 2010-10-05 at 15:46 -0400, Steve Staples wrote:
> >
> > > On Tue, 2010-10-05 at 20:35 +0100, Ashley Sheridan wrote:
> > > > On Tue, 2010-10-05 at 15:28 -0400, chris h wrote:
> > > >
> > > > > Benchmark and find out! :)
> > > > >
> > > > > What are you using this for? Unless you are doing something crazy it
> > > > > probably doesn't matter, and you should pick whichever you feel looks nicer
> > > > > / is easier to code in / etc.
> > > > >
> > > > > Chris H.
> > > > >
> > > > > On Tue, Oct 5, 2010 at 3:23 PM, saeed ahmed <saeed.sas(a)gmail.com> wrote:
> > > > >
> > > > > > $a = 'hey';
> > > > > > $b = 'done';
> > > > > >
> > > > > > $c = $a.$b;
> > > > > > $c = "$a$b";
> > > > > >
> > > > > > which one is faster for echo $c.
> > > > > >
> > > >
> > > >
> > > > As far as I'm aware, the first of the two will be faster, but only just.
> > > > As Saeed mentioned, the difference will be negligible, and unless you
> > > > plan to run a line like that in a loop or something hundreds of
> > > > thousands of times, you probably won't notice any difference.
> > > > Thanks,
> > > > Ash
> > > > http://www.ashleysheridan.co.uk
> > > >
> > > >
> > >
> > >
> > > to be proper, shouldn't it technically be
> > > $c = "{$a}{$b}";
> > >
> > > ??
> > >
> > > Steve.
> > >
> > >
> >
> >
> > It doesn't have to use the braces. The braces only tell PHP exactly
> > where to stop parsing the current variable name. The following examples
> > wouldn't work without them:
> >
> > $var = 'hello ';
> > $arr = array('msg 1'=>'hello','msg 2'=>'world');
> >
> > echo "{$var}world";
> > echo "{$arr['msg 1']}{$arr['msg 2']}";
> >
> > Without the braces, in the first example PHP would look for a variable
> > called $varworld, and in the second it would be looking for a simple
> > scaler called $arr, not the array value you wanted.
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> Ash:
>
> I understand what the {} does, but just like in HTML, it is more proper
> to use lower case for the attributes/elements, and use " (double quotes)
> when wrapping the attributes... but is it not "REQUIRED" to write it in
> that manner... just like it is not required to wrap the variables in {}
> when inside the ""...
>
> that's just me, I tend to try and do that every time...
>
> Steve.
>
>


Not really, it's no more proper than the difference between:

echo 'text' . $var;
or
echo "text$var";

And in some cases, it is required. It is down to preference if you use
them when you don't have to, but personally I'd avoid typing what is
essentially useless fluff.


Thanks,
Ash
http://www.ashleysheridan.co.uk


First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: i wanna join
Next: Variable (Class instantiation) collision