Whacky increment/assignment logic with $foo++ vs ++$foo
Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:01:34 von Daevid Vincent
$num = 123;
$num = $num++;
print $num; //this prints 123 and not 124 ?!!
$num = 123;
$num = ++$num;
print $num; //this prints 124 as expected
$num = 123;
$num++;
print $num; //this prints 124 as expected
$num = 123;
print $num++; //this prints 123 and not 124 ?!!
print $num++; //this NOW prints 124
?>
So then I read the manual because I think I'm loosing my mind and perhaps
it's backwards day and nobody told me:
http://us3.php.net/manual/en/language.operators.increment.ph p
I'm baffled as to the reasoning behind:
"$a++ :: Post-increment :: Returns $a, then increments $a by one."
Why would you EVER want $num = $num++; to give you back the value you
already had? Even if we did $foo = $bar++; I would still logically (and
common sensely) expect $foo to be the increment of $bar!
It also seems counter-intuitive, as I was always lead to believe everything
is processed on the right of an equals sign and then assigned back to the
left side of the equals sign. In this case, it does a mixture of both.
Can someone PLEASE explain why the developers of PHP chose this seemingly
whacky logic?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:06:47 von Ben Dunlap
> Can someone PLEASE explain why the developers of PHP chose this seemingly
> whacky logic?
It mimicks C.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:12:13 von Robert Cummings
Daevid Vincent wrote:
>
> $num = 123;
> $num = $num++;
> print $num; //this prints 123 and not 124 ?!!
>
> $num = 123;
> $num = ++$num;
> print $num; //this prints 124 as expected
>
> $num = 123;
> $num++;
> print $num; //this prints 124 as expected
>
> $num = 123;
> print $num++; //this prints 123 and not 124 ?!!
> print $num++; //this NOW prints 124
> ?>
>
> So then I read the manual because I think I'm loosing my mind and perhaps
> it's backwards day and nobody told me:
> http://us3.php.net/manual/en/language.operators.increment.ph p
>
> I'm baffled as to the reasoning behind:
> "$a++ :: Post-increment :: Returns $a, then increments $a by one."
>
> Why would you EVER want $num = $num++; to give you back the value you
> already had? Even if we did $foo = $bar++; I would still logically (and
> common sensely) expect $foo to be the increment of $bar!
>
> It also seems counter-intuitive, as I was always lead to believe everything
> is processed on the right of an equals sign and then assigned back to the
> left side of the equals sign. In this case, it does a mixture of both.
>
> Can someone PLEASE explain why the developers of PHP chose this seemingly
> whacky logic?
This logic is almost universal in modern programming languages. Some
people do the following:
$index = 1;
foreach( @items as @item )
{
echo '
'
.''.($index++).' | '
.''.$item.' | '
.'
';
}
?>
Some people consider it weird to start at 0 for the above index, because
the index is only used when it is 1 or more. But the equivalent of the
above is the following:
$index = 0;
foreach( @items as @item )
{
echo ''
.''.(++$index).' | '
.''.$item.' | '
.'
';
}
?>
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:18:33 von Tommy Pham
----- Original Message ----
> From: Ben Dunlap
> To: Daevid Vincent
> Cc: php-general@lists.php.net
> Sent: Fri, October 2, 2009 2:06:47 PM
> Subject: Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
>
> > Can someone PLEASE explain why the developers of PHP chose this seemingly
> > whacky logic?
>
> It mimicks C.
>
> Ben
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
Any language based on C has it, such as C++, Java and C#. It gives programmers/developers flexibility when to increment and use its value.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:28:29 von Daevid Vincent
> -----Original Message-----
> From: Robert Cummings [mailto:robert@interjinn.com]
> Sent: Friday, October 02, 2009 2:12 PM
> To: Daevid Vincent
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Whacky increment/assignment logic with
> $foo++ vs ++$foo
>
> Daevid Vincent wrote:
> >
> > $num = 123;
> > $num = $num++;
> > print $num; //this prints 123 and not 124 ?!!
> >
> > $num = 123;
> > $num = ++$num;
> > print $num; //this prints 124 as expected
> >
> > $num = 123;
> > $num++;
> > print $num; //this prints 124 as expected
> >
> > $num = 123;
> > print $num++; //this prints 123 and not 124 ?!!
> > print $num++; //this NOW prints 124
> > ?>
> >
> > So then I read the manual because I think I'm loosing my
> mind and perhaps
> > it's backwards day and nobody told me:
> > http://us3.php.net/manual/en/language.operators.increment.ph p
> >
> > I'm baffled as to the reasoning behind:
> > "$a++ :: Post-increment :: Returns $a, then increments $a by one."
> >
> > Why would you EVER want $num = $num++; to give you back the
> value you
> > already had? Even if we did $foo = $bar++; I would still
> logically (and
> > common sensely) expect $foo to be the increment of $bar!
> >
> > It also seems counter-intuitive, as I was always lead to
> believe everything
> > is processed on the right of an equals sign and then
> assigned back to the
> > left side of the equals sign. In this case, it does a
> mixture of both.
> >
> > Can someone PLEASE explain why the developers of PHP chose
> this seemingly
> > whacky logic?
>
> This logic is almost universal in modern programming languages. Some
> people do the following:
>
>
>
> $index = 1;
> foreach( @items as @item )
> {
> echo '
'
> .''.($index++).' | '
> .''.$item.' | '
> .'
';
> }
>
> ?>
>
> Some people consider it weird to start at 0 for the above
> index, because
> the index is only used when it is 1 or more. But the
> equivalent of the
> above is the following:
>
>
>
> $index = 0;
> foreach( @items as @item )
> {
> echo ''
> .''.(++$index).' | '
> .''.$item.' | '
> .'
';
> }
>
> ?>
My problem isn't with $foo++ vs ++$foo per say. I use pre/post all the time.
My issue is that I see no reason to do the ASSIGNMENT FIRST and THEN
INCREMENT.
That's just counter intuitive. In the case of $foo = $num++, everything to
the right of the = should be computed FIRST and THEN handed off to the left
side. This particular expression (and I'm unaware of any other PHP
expression that works this way) chooses to do some "FM" (f'n magic) and do
an assignment FIRST and THEN increment.
All of this is just an academic discussion at this point, as I'm sure there
is WAY too much code out there to even consider changing this behaviour to a
more sane one. And I'm told it mimics other languages (wright or wrong, it
is what it is).
Personally I've never (in almost 20 years) done an assignment like "$foo =
$foo++" as I always use just "$foo++" or "$foo += 1" or something, hence the
reason today is the day a co-worker stumbled upon this and was as confused
as I was until I figured it out.
D.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:37:48 von Ben Dunlap
> My issue is that I see no reason to do the ASSIGNMENT FIRST and THEN
> INCREMENT.
>
> That's just counter intuitive. In the case of $foo = $num++, everything to
> the right of the = should be computed FIRST and THEN handed off to the left
> side. This particular expression (and I'm unaware of any other PHP
> expression that works this way) chooses to do some "FM" (f'n magic) and do
> an assignment FIRST and THEN increment.
It's not the expression that works that way -- it's the operator. The
post-increment operator /always/ does its work after the expression
that it's in has been evaluated.
Are you thinking it would be more intuitive if that operator departed
from its normal behavior in this one special case?
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:42:23 von Daniel Brown
On Fri, Oct 2, 2009 at 17:28, Daevid Vincent wrote:
>
> Personally I've never (in almost 20 years) done an assignment like "$foo =
> $foo++" as I always use just "$foo++" or "$foo += 1" or something, hence the
> reason today is the day a co-worker stumbled upon this and was as confused
> as I was until I figured it out.
And that's exactly the point: why would you assign this variable
to another in the first place? It's just using up resources and
creating more overhead in the application. The reason it exists is
for processing, really, not assigning; and the ability to do it before
or after is appropriate for certain conditions. However, keep in mind
that you don't even need to do anything with the variable to increment
it:
$n = 1;
$n++;
echo $n."\n"; // 2
?>
The $var++ vs. ++$var (or, conversely, $var-- vs. --$var)
comparison isn't best described by the most commonly-used incrementing
scenario:
$a = array('foo' => 'bar', 'apple' => 'red', 'lime' => 'green', 'wife'
=> 'pain in ass', 'lemon' => 'yellow');
for($i=0;$i
// ....
}
?>
.... but rather by something just slightly more advanced:
$num = file_get_contents('visitcount.txt');
if(isset($_GET['countme'])) {
echo "You are visitor #".++$num."
\n";
file_put_contents($num);
}
?>
If you were to use $num++, it would echo out the current number,
THEN increment the value. In this example, it increments the value,
THEN echoes it out. The placement of the signs (plus or minus) is the
giveaway: if it's before the variable, it's modified before being
processed. If it's after, then it's evaluated immediately after the
variable is processed.
--
daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:45:21 von Daniel Brown
On Fri, Oct 2, 2009 at 17:42, Daniel Brown wrote:
>
> =A0 =A0.... but rather by something just slightly more advanced:
>
>
> $num =3D file_get_contents('visitcount.txt');
> if(isset($_GET['countme'])) {
> =A0 =A0echo "You are visitor #".++$num."
\n";
> =A0 =A0file_put_contents($num);
> }
> ?>
Converse example (sorry, forgot to add it):
$num =3D file_get_contents('visitcount.txt');
if(isset($_GET['countme'])) {
echo "There were ".$num++." people here before you.
\n";
file_put_contents($num);
}
?>
--=20
daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:53:56 von Ben Dunlap
On Fri, Oct 2, 2009 at 2:37 PM, Ben Dunlap wrote:
>> My issue is that I see no reason to do the ASSIGNMENT FIRST and THEN
>> INCREMENT.
>>
>> That's just counter intuitive. In the case of $foo = $num++, everything to
>> the right of the = should be computed FIRST and THEN handed off to the left
>> side. This particular expression (and I'm unaware of any other PHP
>> expression that works this way) chooses to do some "FM" (f'n magic) and do
>> an assignment FIRST and THEN increment.
>
> It's not the expression that works that way -- it's the operator. The
> post-increment operator /always/ does its work after the expression
> that it's in has been evaluated.
>
> Are you thinking it would be more intuitive if that operator departed
> from its normal behavior in this one special case?
On further thought I do see why this one special case is a little
mind-blowing. What the heck /is/ supposed to happen when you do this:
$a = 2;
$a = $a++;
echo $a;
Seems like any way you slice it the output should be 3. I guess what's
revealed here is that, as far as PHP is concerned, the $a on the right
side of the assignment expression is something like a temporary copy
of the variable in the current scope. So the assignment gets
evaluated, and then ++ operates on that "copy" and the result is
discarded.
Honestly I think the only reason anyone would write an expression like
that is either to fake out the compiler or because they don't properly
understand the use of a unary operator. Or rather, of the
increment/decrement operators, because no other unary operator
actually changes the thing it operates on (AFAIK), which makes ++ and
-- doubly weird.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 02.10.2009 23:58:14 von Ben Dunlap
> mind-blowing. What the heck /is/ supposed to happen when you do this:
>
> =A0 =A0$a =3D 2;
> =A0 =A0$a =3D $a++;
> =A0 =A0echo $a;
>
> Seems like any way you slice it the output should be 3. I guess what's
.... and, in fact, that /is/ how C behaves. The following code:
int a =3D 2;
a =3D a++;
printf("a =3D [%d]\n", a);
Will output "a =3D [3]". At least on Ubuntu 9 using gcc 4.3.3.
So I retract my initial terse reply and apologize for misunderstanding
your question.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 00:01:40 von Ben Dunlap
>> Seems like any way you slice it the output should be 3. I guess what's
>
> ... and, in fact, that /is/ how C behaves. The following code:
Whereas Perl (v5.10.0) handles the construct the same way that PHP
does. Curiouser and curiouser.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 00:22:47 von Daevid Vincent
=20
> -----Original Message-----
> From: Ben Dunlap [mailto:bdunlap@agentintellect.com]=20
> Sent: Friday, October 02, 2009 2:58 PM
> To: php-general@lists.php.net; Daevid Vincent
> Subject: Re: [PHP] Whacky increment/assignment logic with=20
> $foo++ vs ++$foo
>=20
> > mind-blowing. What the heck /is/ supposed to happen when=20
> you do this:
> >
> > =A0 =A0$a =3D 2;
> > =A0 =A0$a =3D $a++;
> > =A0 =A0echo $a;
> >
> > Seems like any way you slice it the output should be 3. I=20
> guess what's
>=20
> ... and, in fact, that /is/ how C behaves. The following code:
>=20
> int a =3D 2;
> a =3D a++;
> printf("a =3D [%d]\n", a);
>=20
> Will output "a =3D [3]". At least on Ubuntu 9 using gcc 4.3.3.
>=20
> So I retract my initial terse reply and apologize for misunderstanding
> your question.
>=20
> Ben
EXACTLY! :)
God (or diety of your choice) bless you for "getting" what I'm saying =
and
proving that it's not "C" like either. That just adds credence to my/our
argument.
d
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 00:30:11 von Mari Masuda
On Oct 2, 2009, at 15:22, Daevid Vincent wrote:
>
>
>> -----Original Message-----
>> From: Ben Dunlap [mailto:bdunlap@agentintellect.com]
>> Sent: Friday, October 02, 2009 2:58 PM
>> To: php-general@lists.php.net; Daevid Vincent
>> Subject: Re: [PHP] Whacky increment/assignment logic with
>> $foo++ vs ++$foo
>>
>>> mind-blowing. What the heck /is/ supposed to happen when
>> you do this:
>>>
>>> $a = 2;
>>> $a = $a++;
>>> echo $a;
>>>
>>> Seems like any way you slice it the output should be 3. I
>> guess what's
>>
>> ... and, in fact, that /is/ how C behaves. The following code:
>>
>> int a = 2;
>> a = a++;
>> printf("a = [%d]\n", a);
>>
>> Will output "a = [3]". At least on Ubuntu 9 using gcc 4.3.3.
>>
>> So I retract my initial terse reply and apologize for
>> misunderstanding
>> your question.
>>
>> Ben
>
> EXACTLY! :)
>
> God (or diety of your choice) bless you for "getting" what I'm
> saying and
> proving that it's not "C" like either. That just adds credence to
> my/our
> argument.
>
>
> d
>
I think if you rewrote the above example as
int a = 2;
b = a++;
printf("b = [%d]\n", b);
"b" would be 2 when printed. However, after the second line (b = a+
+;) finished executing, "a" would then be 3.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 00:44:01 von Ben Dunlap
> =A0 =A0 =A0 =A0int a =3D 2;
> =A0 =A0 =A0 =A0b =3D a++;
> =A0 =A0 =A0 =A0printf("b =3D [%d]\n", b);
>
> "b" would be 2 when printed. =A0However, after the second line (b =3D a++=
;)
> finished executing, "a" would then be 3.
Sure, but that code is perfectly clear. It's the odd special case
where you assign the variable to itself, that's ambiguous. Like Daevid
said, academic at this point -- but it might shed light on some
compiler-design decisions that I don't have the vocabulary for.
OTOH it could just a be a unique case with unpredictable results.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 00:58:25 von Joost
"Daevid Vincent" wrote:
>
>
>> -----Original Message-----
>> From: Ben Dunlap [mailto:bdunlap@agentintellect.com]
>> Sent: Friday, October 02, 2009 2:58 PM
>> To: php-general@lists.php.net; Daevid Vincent
>> Subject: Re: [PHP] Whacky increment/assignment logic with
>> $foo++ vs ++$foo
>>
>> > mind-blowing. What the heck /is/ supposed to happen when
>> you do this:
>> >
>> > $a = 2;
>> > $a = $a++;
>> > echo $a;
>> >
>> > Seems like any way you slice it the output should be 3. I
>> guess what's
>>
>> ... and, in fact, that /is/ how C behaves. The following code:
>>
>> int a = 2;
>> a = a++;
>> printf("a = [%d]\n", a);
>>
>> Will output "a = [3]". At least on Ubuntu 9 using gcc 4.3.3.
>>
>> So I retract my initial terse reply and apologize for misunderstanding
>> your question.
>>
>> Ben
>
> EXACTLY! :)
>
> God (or diety of your choice) bless you for "getting" what I'm saying and
> proving that it's not "C" like either. That just adds credence to my/our
> argument.
>
>
> d
Well, Daevid,
I still do appreciate the difference between
$a = 1;
$b = $a++;
// $b should be 1
and
$a = $a++;
$b = $a;
// $b should be 2
unless PHP manages to exhibit some overlay problem (read: bug) for $a in
the 2nd case.
True?
joost.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 15:57:33 von Ralph Deffke
Ben,
might be intersting to consider that in ur c axample u r working with a pure
memory position, while php works with references. thry it with pointers it
I'm pretty shure u get the same result as in PHP.
I'm not shure, because I don't work in perl, but doesn't per work on
references as well ?
ralph_deffke@yahoo.de
"Ben Dunlap" wrote in message
news:7997e80e0910021458h20ebd75dtfc51f9264f35171b@mail.gmail .com...
> mind-blowing. What the heck /is/ supposed to happen when you do this:
>
> $a = 2;
> $a = $a++;
> echo $a;
>
> Seems like any way you slice it the output should be 3. I guess what's
.... and, in fact, that /is/ how C behaves. The following code:
int a = 2;
a = a++;
printf("a = [%d]\n", a);
Will output "a = [3]". At least on Ubuntu 9 using gcc 4.3.3.
So I retract my initial terse reply and apologize for misunderstanding
your question.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 16:42:04 von TedD
At 2:01 PM -0700 10/2/09, Daevid Vincent wrote:
>Why would you EVER want $num = $num++; to give you back the value you
>already had? Even if we did $foo = $bar++; I would still logically (and
>common sensely) expect $foo to be the increment of $bar!
You are right -- one should never structure a statement like that.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 16:45:00 von TedD
At 5:12 PM -0400 10/2/09, Robert Cummings wrote:
>Daevid Vincent wrote:
>>
>>$num = 123;
>>$num = $num++;
>>print $num; //this prints 123 and not 124 ?!!
>>
>>$num = 123;
>>$num = ++$num;
>>print $num; //this prints 124 as expected
>>
>>$num = 123;
>>$num++;
>>print $num; //this prints 124 as expected
>>
>>$num = 123;
>>print $num++; //this prints 123 and not 124 ?!!
>>print $num++; //this NOW prints 124
>>?>
>>
>>So then I read the manual because I think I'm loosing my mind and perhaps
>>it's backwards day and nobody told me:
>>http://us3.php.net/manual/en/language.operators.increment. php
>>
>>I'm baffled as to the reasoning behind:
>>"$a++ :: Post-increment :: Returns $a, then increments $a by one."
>>
>>Why would you EVER want $num = $num++; to give you back the value you
>>already had? Even if we did $foo = $bar++; I would still logically (and
>>common sensely) expect $foo to be the increment of $bar!
>>
>>It also seems counter-intuitive, as I was always lead to believe everything
>>is processed on the right of an equals sign and then assigned back to the
>>left side of the equals sign. In this case, it does a mixture of both.
>>
>>Can someone PLEASE explain why the developers of PHP chose this seemingly
>>whacky logic?
>
>This logic is almost universal in modern programming languages. Some
>people do the following:
>
>
>
>$index = 1;
>foreach( @items as @item )
>{
> echo '
'
> .''.($index++).' | '
> .''.$item.' | '
> .'
';
>}
>
>?>
>
>Some people consider it weird to start at 0 for the above index,
>because the index is only used when it is 1 or more. But the
>equivalent of the above is the following:
>
>
>
>$index = 0;
>foreach( @items as @item )
>{
> echo ''
> .''.(++$index).' | '
> .''.$item.' | '
> .'
';
>}
>
>?>
>
>Cheers,
>Rob.
>--
Rob:
Your examples are fine -- no problems whatsoever. I do the same thing myself.
The problem I see here is with the original statement of:
$num = $$num++;
That's just bad form -- it doesn't do anything except inject confusion.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 16:47:35 von TedD
At 2:28 PM -0700 10/2/09, Daevid Vincent wrote:
>My problem isn't with $foo++ vs ++$foo per say. I use pre/post all the time.
>
>My issue is that I see no reason to do the ASSIGNMENT FIRST and THEN
>INCREMENT.
I see your point exactly.
The problem is with the statement of:
$num = $num++;
That statement is just bad form. The increment is never realized.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 16:49:11 von TedD
At 5:42 PM -0400 10/2/09, Daniel Brown wrote:
> >
>
> If you were to use $num++, it would echo out the current number,
>THEN increment the value. In this example, it increments the value,
>THEN echoes it out. The placement of the signs (plus or minus) is the
>giveaway: if it's before the variable, it's modified before being
>processed. If it's after, then it's evaluated immediately after the
>variable is processed.
That's absolutely true.
The problem here is in the statement of:
$num = $num++;
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 16:52:36 von TedD
At 2:53 PM -0700 10/2/09, Ben Dunlap wrote:
> $a = 2;
> $a = $a++;
> echo $a;
>Honestly I think the only reason anyone would write an expression like
>that is either to fake out the compiler or because they don't properly
>understand the use of a unary operator. Or rather, of the
>increment/decrement operators, because no other unary operator
>actually changes the thing it operates on (AFAIK), which makes ++ and
>-- doubly weird.
>
>Ben
Honestly, I think that the only reason why one would write that is
that they don't know any better.
Why do this:
$a = $a++;
when
$a++;
will do what is intended?
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 17:19:22 von Daniel Brown
On Sat, Oct 3, 2009 at 10:49, tedd wrote:
>
> That's absolutely true.
>
> The problem here is in the statement of:
>
> $num = $num++;
Yeah, I understood Daevid's email a bit better *after* I sent
mine. Then I was hoping no one noticed.
--
daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 03.10.2009 21:11:37 von Andrea Giammarchi
--_d68c250b-d417-4af7-8134-875be5784692_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
> ... and=2C in fact=2C that /is/ how C behaves. The following code:
>=20
> int a =3D 2=3B
> a =3D a++=3B
> printf("a =3D [%d]\n"=2C a)=3B
>=20
> Will output "a =3D [3]". At least on Ubuntu 9 using gcc 4.3.3.
>=20
> So I retract my initial terse reply and apologize for misunderstanding
> your question.
>=20
> Ben
It's not that difficult to understand ... we are talking about a scripting =
language as PHP is
The code you wrote for C is not the equivalent while this is:
int a =3D 2=2C b=3B
b =3D a++=3B
printf("b =3D [%d]\n"=2C b)=3B
and b will be exactly 2.
In PHP you are not referencing that variable=2C you are overwriting variabl=
e $a with an integer=2C 2=2C and that's it.
The incremented integer=2C 3=2C is simply lost in the silly logic of the op=
eration. The equivalent of your C code=2C in PHP=2C would be just this:
$a =3D 2=3B
$a++=3B
print $a=3B // of course is 3=2C the initial $a is not lost
or=2C to be more explicit ...
$a =3D 2=3B
($a =3D &$a) and $a++=3B
print $a=3B
Regards
=0A=
____________________________________________________________ _____=0A=
Keep your friends updated=97even when you=92re not signed in.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_5:092=
010=
--_d68c250b-d417-4af7-8134-875be5784692_--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 04.10.2009 03:06:23 von Lupus Michaelis
Ben Dunlap wrote:
> ... and, in fact, that /is/ how C behaves. The following code:
No, that's implementation's behaviour. AFAIK, the normative document
give to compiler the behaviour implementation. So, it can do
optimization, that gives strange behaviour for a people how think
increment operators had a wall defined behaviour.
> int a = 2;
> a = a++;
> printf("a = [%d]\n", a);
>
> Will output "a = [3]". At least on Ubuntu 9 using gcc 4.3.3.
That's gcc way :)
--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 14:51:17 von Bob McConnell
From: Joost [mailto:joost.t.hart@planet.nl]=20
> "Daevid Vincent" wrote:
>>> From: Ben Dunlap [mailto:bdunlap@agentintellect.com]
> $a =3D $a++;
I just think this is an ambiguous line of code that wasn't thought
through. The presence of the postfix operator makes the result
undefined, no matter what language you are using. It will be an accident
if you get the results you are expecting.
Bob McConnell
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 15:00:01 von Andrea Giammarchi
--_c67e51e5-1ba0-4af8-bf5c-e373f5e38f6b_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> It will be an accident
> if you get the results you are expecting.
I agree that the operation is illogical=2C I must disagree about accidents.
In PHP that operation will mean assign to the new $a variable the value ret=
urned from the other $a variable before the increment.
There is no mystery here=2C imho.
Regards
=0A=
____________________________________________________________ _____=0A=
Windows Live: Keep your friends up to date with what you do online.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_1:092=
010=
--_c67e51e5-1ba0-4af8-bf5c-e373f5e38f6b_--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 15:28:05 von Paul M Foster
On Tue, Oct 06, 2009 at 08:51:17AM -0400, Bob McConnell wrote:
> From: Joost [mailto:joost.t.hart@planet.nl]
> > "Daevid Vincent" wrote:
> >>> From: Ben Dunlap [mailto:bdunlap@agentintellect.com]
>
> > $a = $a++;
>
> I just think this is an ambiguous line of code that wasn't thought
> through. The presence of the postfix operator makes the result
> undefined, no matter what language you are using. It will be an accident
> if you get the results you are expecting.
The behavior of the ++ operator is the invention of Kernighan and Ritchie.
I don't imagine they ever foresaw anyone doing something as silly as
a = a++;
except under the rarest of circumstances.
Paul
--
Paul M. Foster
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 15:48:28 von Martin Scotta
--000325558fd62f5ae10475447c21
Content-Type: text/plain; charset=ISO-8859-1
On Tue, Oct 6, 2009 at 10:28 AM, Paul M Foster wrote:
> On Tue, Oct 06, 2009 at 08:51:17AM -0400, Bob McConnell wrote:
>
> > From: Joost [mailto:joost.t.hart@planet.nl]
> > > "Daevid Vincent" wrote:
> > >>> From: Ben Dunlap [mailto:bdunlap@agentintellect.com]
> >
> > > $a = $a++;
> >
> > I just think this is an ambiguous line of code that wasn't thought
> > through. The presence of the postfix operator makes the result
> > undefined, no matter what language you are using. It will be an accident
> > if you get the results you are expecting.
>
> The behavior of the ++ operator is the invention of Kernighan and Ritchie.
> I don't imagine they ever foresaw anyone doing something as silly as
>
> a = a++;
>
> except under the rarest of circumstances.
>
> Paul
>
> --
> Paul M. Foster
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
No matter how silly it can looks like (a = a++) it is still completely valid
code and it SHOULD run without problems.
If we analyse any portion of code (like a simple assigment) out of context
it'll always looks like this, just silly (in this case it's really really
silly).
One point here, that nobody mention, is the side effect.
++$i has a totaly different side effect than $i++
Sometimes you don't need the side effect but in other situations it really
matter.
Does these behaves exactly?
for($i=0; $i<10; ++$i)
for($i=0; $i<10; $i++)
There is no side effect on the incremental section because the result is not
evaluated.
and what about these?
$array[ $index++ ] = $elem;
$array[ ++$index ] = $elem;
You can read more about the side effect at
http://en.wikipedia.org/wiki/Side_effect_%28computer_science %29
--
Martin Scotta
--000325558fd62f5ae10475447c21--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 15:56:29 von Andrea Giammarchi
--_e652a576-8889-4b3c-9f0a-d4a7b9a12f9b_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> Does these behaves exactly?
> for($i=3D0=3B $i<10=3B ++$i)
> for($i=3D0=3B $i<10=3B $i++)
different benchmarks showed ++$i is usually faster than $i++
In that loop case=2C yes=2C what's happen internally is exactly the same=2C=
$i will be from 0 to 9=2C in the other case obviously is not the same.
but pre increment and post increment are truly basic stuff ... I don-t see =
all this need to study the case=2C it's pretty simple=2C as is that operati=
on=2C in PHP.
In other languages=2C could have been the same=2C rarely in scripting langu=
ages though=2C at least those with still primitive scalar values (int=2C fl=
oat=2C string=2C bool)
Regards
=0A=
____________________________________________________________ _____=0A=
Windows Live Hotmail: Your friends can get your Facebook updates=2C right f=
rom Hotmail=AE.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_4:092=
009=
--_e652a576-8889-4b3c-9f0a-d4a7b9a12f9b_--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 18:15:54 von TedD
At 3:56 PM +0200 10/6/09, Andrea Giammarchi wrote:
> > Does these behaves exactly?
>> for($i=0; $i<10; ++$i)
>> for($i=0; $i<10; $i++)
>
>different benchmarks showed ++$i is usually faster than $i++
"Faster" is a relative term that is becoming more meaningless each year.
Considering that "speed" is increasing and "memory" prices are
dropping exponentially, both of those are becoming less and less
important in design considerations (my opinion).
The speeds of the Crays of yesteryear we are now holding in our hands
as cell phones. The memory we are buying today is literally fractions
of a cent of the tens of thousands of dollars we spent some 20 years
ago.
I venture to claim the time it took me to write this email (and for
you to read it) was longer than the total time saved between using
++$i vs $i++ for all the php scripts in the world over the remaining
life span of PHP.
Interesting "food for thought", huh?
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 18:25:06 von Andrea Giammarchi
--_8b01bb14-bb3a-41d7-bd28-7b8e4aa80b1f_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
er ... tedd=2C whatever=2C usually ++i is faster in almost every language=
=2C and even C developers could use these kind of micro optimizations.
Speed=2C even in this SuperCPU era=2C is still relevant=2C we would not nee=
d benchmark to compare programming languages for each purpose.
Of course in a crappy application=2C the usage of ++i rather than i++ won't=
make any difference=2C but specially for that kind of for loop where there=
is absolutely no harm or side-effect using ++i rather than i++ ... if ++i =
could be 0.0001% nobody have a valid reason to avoid it.
Put in this way: I need to do the same thing=2C one could be better ... why=
on earth should I use the other way?
I just develop applications=2C where I can micro-optimize=2C I do it ... I =
have never had speed problems=2C but maybe I am just lucky.
Regards
> Date: Tue=2C 6 Oct 2009 12:15:54 -0400
> To: php-general@lists.php.net
> From: tedd.sperling@gmail.com
> Subject: RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$fo=
o
>=20
> At 3:56 PM +0200 10/6/09=2C Andrea Giammarchi wrote:
> > > Does these behaves exactly?
> >> for($i=3D0=3B $i<10=3B ++$i)
> >> for($i=3D0=3B $i<10=3B $i++)
> >
> >different benchmarks showed ++$i is usually faster than $i++
>=20
> "Faster" is a relative term that is becoming more meaningless each year.
>=20
> Considering that "speed" is increasing and "memory" prices are=20
> dropping exponentially=2C both of those are becoming less and less=20
> important in design considerations (my opinion).
>=20
> The speeds of the Crays of yesteryear we are now holding in our hands=20
> as cell phones. The memory we are buying today is literally fractions=20
> of a cent of the tens of thousands of dollars we spent some 20 years=20
> ago.
>=20
> I venture to claim the time it took me to write this email (and for=20
> you to read it) was longer than the total time saved between using=20
> ++$i vs $i++ for all the php scripts in the world over the remaining=20
> life span of PHP.
>=20
> Interesting "food for thought"=2C huh?
>=20
> Cheers=2C
>=20
> tedd
>=20
> --=20
> -------
> http://sperling.com http://ancientstones.com http://earthstones.com
>=20
> --=20
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe=2C visit: http://www.php.net/unsub.php
>=20
=0A=
____________________________________________________________ _____=0A=
Windows Live: Make it easier for your friends to see what you=92re up to on=
Facebook.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_2:092=
009=
--_8b01bb14-bb3a-41d7-bd28-7b8e4aa80b1f_--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 19:31:53 von Eddie Drapkin
On Tue, Oct 6, 2009 at 12:25 PM, Andrea Giammarchi wro=
te:
>
> er ... tedd, whatever, usually ++i is faster in almost every language, an=
d even C developers could use these kind of micro optimizations.
>
> Speed, even in this SuperCPU era, is still relevant, we would not need be=
nchmark to compare programming languages for each purpose.
>
> Of course in a crappy application, the usage of ++i rather than i++ won't=
make any difference, but specially for that kind of for loop where there i=
s absolutely no harm or side-effect using ++i rather than i++ ... if ++i co=
uld be 0.0001% nobody have a valid reason to avoid it.
>
> Put in this way: I need to do the same thing, one could be better ... why=
on earth should I use the other way?
>
> I just develop applications, where I can micro-optimize, I do it ... I ha=
ve never had speed problems, but maybe I am just lucky.
>
> Regards
>
>> Date: Tue, 6 Oct 2009 12:15:54 -0400
>> To: php-general@lists.php.net
>> From: tedd.sperling@gmail.com
>> Subject: RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$f=
oo
>>
>> At 3:56 PM +0200 10/6/09, Andrea Giammarchi wrote:
>> > Â > Does these behaves exactly?
>> >> Â for($i=3D0; $i<10; ++$i)
>> >> Â for($i=3D0; $i<10; $i++)
>> >
>> >different benchmarks showed ++$i is usually faster than $i++
>>
>> "Faster" is a relative term that is becoming more meaningless each year.
>>
>> Considering that "speed" is increasing and "memory" prices are
>> dropping exponentially, both of those are becoming less and less
>> important in design considerations (my opinion).
>>
>> The speeds of the Crays of yesteryear we are now holding in our hands
>> as cell phones. The memory we are buying today is literally fractions
>> of a cent of the tens of thousands of dollars we spent some 20 years
>> ago.
>>
>> I venture to claim the time it took me to write this email (and for
>> you to read it) was longer than the total time saved between using
>> ++$i vs $i++ for all the php scripts in the world over the remaining
>> life span of PHP.
>>
>> Interesting "food for thought", huh?
>>
>> Cheers,
>>
>> tedd
>>
>> --
>> -------
>> http://sperling.com  http://ancientstones.com  http://earthsto=
nes.com
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
> ____________________________________________________________ _____
> Windows Live: Make it easier for your friends to see what youâ=99re =
up to on Facebook.
> http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/=
social-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL: en-xm:SI_SB_2:0=
92009
The problem with PHP micro-optimizations like that or ' vs. " is that
PHP rarely bottlenecks a PHP/MySQL application (actually if you run a
dedicated box, see how often your CPU hits 100%, it won't be very
often for the vast majority of PHP/MySQL sites which are more likely
to be disk i/o bound than cpu bound). The time spent in PHP (by PHP,
not by the database) for page generation is completely negligible and
invisible behind the overhead of network protocols for the vast
majority of sites or drowned in the performance killing of SQL. I
would probably go so far as to say it doesn't matter how quickly your
PHP runs; you'd save as much time tuning one slow query as you would
micro-optimizing every line of your codebase. Furthermore, the amount
of time micro-optimization takes up (going through old code, I mean)
could be better spent doing something that actually does increase your
performance, like implementing a search engine or memcached. Going
forward, if you're aware that ++i and i++ are the same for your
application and ++i is a single php opcode faster (which I don't know
if it's even measurable, that difference), sure go ahead and use ++i
but it's certainly not worth serious thought or developer time.
My two cents.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 19:44:18 von TedD
At 6:25 PM +0200 10/6/09, Andrea Giammarchi wrote:
>er ... tedd, whatever, usually ++i is faster in almost every
>language, and even C developers could use these kind of micro
>optimizations.
>
>Speed, even in this SuperCPU era, is still relevant, we would not
>need benchmark to compare programming languages for each purpose.
>
>Of course in a crappy application, the usage of ++i rather than i++
>won't make any difference, but specially for that kind of for loop
>where there is absolutely no harm or side-effect using ++i rather
>than i++ ... if ++i could be 0.0001% nobody have a valid reason to
>avoid it.
>
>Put in this way: I need to do the same thing, one could be better
>... why on earth should I use the other way?
>
>I just develop applications, where I can micro-optimize, I do it ...
>I have never had speed problems, but maybe I am just lucky.
Andrea:
I think you missed my point.
First, you do whatever you want -- do you whatever makes you feel
comfortable. I'm not trying to change your ways at all.
Second, to the contrary -- all I am saying is if you have a
preference in using ++$i or $i++, then use it. But to say the reason
why you use it is because of speed is becoming less of an issue than
it was. So much so, that for people to argue either side is rather
pointless. There is no significant difference.
Sure we will continue to benchmark the speed of different languages
for comparisons, we have a long history/habit of doing that. But that
too is becoming less important than it was for what was significant
yesterday is not significant today and will be even less so tomorrow.
For example, the Human Gnome Project was first thought to be a
project that would take at least 15 years, but it was finished in 5.
Why? Because the original projections were based upon the computing
power of the day and didn't take into account advances in speed and
memory.
So while we can debate computing considerations of today, tomorrow
those will be less important. That was the point I was making. Why
not focus on things that make significant difference and let the
insignificant fade into history.
Look on the bright side, you can tell your grand children "I remember
when ++i was faster than i++" and they'll wonder if Mom and Dad were
right when they talked about putting you in a rest home.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 19:46:36 von Jay Blanchard
[snip]
....micro optimizations...
[/snip]
And in the land of micro optimization you would likely never see the
following;
$a =3D $a++;
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 19:47:38 von TedD
>
>The problem with PHP micro-optimizations like that or ' vs. " is that
>PHP rarely bottlenecks a PHP/MySQL application (actually if you run a
>dedicated box, see how often your CPU hits 100%, it won't be very
>often for the vast majority of PHP/MySQL sites which are more likely
>to be disk i/o bound than cpu bound). The time spent in PHP (by PHP,
>not by the database) for page generation is completely negligible and
>invisible behind the overhead of network protocols for the vast
>majority of sites or drowned in the performance killing of SQL. I
>would probably go so far as to say it doesn't matter how quickly your
>PHP runs; you'd save as much time tuning one slow query as you would
>micro-optimizing every line of your codebase. Furthermore, the amount
>of time micro-optimization takes up (going through old code, I mean)
>could be better spent doing something that actually does increase your
>performance, like implementing a search engine or memcached. Going
>forward, if you're aware that ++i and i++ are the same for your
>application and ++i is a single php opcode faster (which I don't know
>if it's even measurable, that difference), sure go ahead and use ++i
>but it's certainly not worth serious thought or developer time.
>
>My two cents.
Eddie:
And thanks for supporting my point.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 19:57:22 von TedD
At 12:46 PM -0500 10/6/09, Jay Blanchard wrote:
>[snip]
>...micro optimizations...
>[/snip]
>
>And in the land of micro optimization you would likely never see the
>following;
>
>$a = $a++;
I must live in the land of micro optimization.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 20:08:14 von TedD
At 10:48 AM -0300 10/6/09, Martin Scotta wrote:
>No matter how silly it can looks like (a = a++) it is still completely valid
>code and it SHOULD run without problems.
Yeah, it's a valid as:
$a = $a;
and does the same thing, which is nothing.
If you want a statement that does something, then use:
$a = ++$a;
or simply:
$a++;
or
++$a;
Any of those will increment $a, whereas ($a = $a++;) does nothing.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 20:11:31 von Tommy Pham
----- Original Message ----
> From: tedd
> To: Martin Scotta ; Paul M Foster
> Cc: php-general@lists.php.net
> Sent: Tue, October 6, 2009 11:08:14 AM
> Subject: Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
>
> At 10:48 AM -0300 10/6/09, Martin Scotta wrote:
> > No matter how silly it can looks like (a = a++) it is still completely valid
> > code and it SHOULD run without problems.
>
> Yeah, it's a valid as:
>
> $a = $a;
>
> and does the same thing, which is nothing.
>
> If you want a statement that does something, then use:
>
> $a = ++$a;
>
> or simply:
>
> $a++;
>
> or
>
> ++$a;
>
> Any of those will increment $a, whereas ($a = $a++;) does nothing.
>
> Cheers,
>
> tedd
>
> -- -------
> http://sperling.com http://ancientstones.com http://earthstones.com
>
> -- PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
I find it interesting for a discussion to go on this long for something as
$a = $a++;
which should have never happened in the first place ;)
Regards,
Tommy
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 20:31:15 von TedD
At 11:11 AM -0700 10/6/09, Tommy Pham wrote:
>
>I find it interesting for a discussion to go on this long for something as
>$a = $a++;
>which should have never happened in the first place ;)
>
>Regards,
>Tommy
Hey, we're programmers. We waste time for a living.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 20:34:17 von Jay Blanchard
[snip]
I find it interesting for a discussion to go on this long for something
as
$a =3D $a++;
[/snip]
You think that is interesting? Start a conversation about these=20
{}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 20:44:10 von Ashley Sheridan
--=-5XrxqEGT7PCJC0cQlgGT
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Tue, 2009-10-06 at 13:34 -0500, Jay Blanchard wrote:
> [snip]
> I find it interesting for a discussion to go on this long for something
> as
> $a = $a++;
> [/snip]
>
> You think that is interesting? Start a conversation about these
>
> {}
>
Now they actually make sense! I've used those as placeholders in
scripts, should I foresee an area where something may be expanded in the
future.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-5XrxqEGT7PCJC0cQlgGT--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 20:46:13 von israelekpo
--00504502c5ed0a873c047548a57b
Content-Type: text/plain; charset=UTF-8
On Tue, Oct 6, 2009 at 2:34 PM, Jay Blanchard wrote:
> [snip]
> I find it interesting for a discussion to go on this long for something
> as
> $a = $a++;
> [/snip]
>
> You think that is interesting? Start a conversation about these
>
> {}
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
You guys are very funny!
Its very funny how so many people are fired up and ready to strike by
something this miniscule.
Speaking of starting a conversation, what do you think about the "goto"
construct introduced just recently?
--
"Good Enough" is not good enough.
To give anything less than your best is to sacrifice the gift.
Quality First. Measure Twice. Cut Once.
--00504502c5ed0a873c047548a57b--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 06.10.2009 20:50:24 von Daniel Brown
On Tue, Oct 6, 2009 at 14:46, Israel Ekpo wrote:
>
> Speaking of starting a conversation, what do you think about the "goto"
> construct introduced just recently?
Better yet: what do you all think of folks hijacking threads?
--
daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Looking for hosting or dedicated servers? Ask me how we can fit your budget!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 00:59:54 von Andrea Giammarchi
--_29f65477-0bf0-4c8f-96f1-d750f69fa301_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> So while we can debate computing considerations of today=2C tomorrow=20
> those will be less important. That was the point I was making. Why=20
> not focus on things that make significant difference and let the=20
> insignificant fade into history.
I tendentiously focus on all things able to make=2C all together=2C even mo=
re significant difference.
Some micro-optimization=2C used as common code style=2C can make the entire=
application or the specific performance critical task=2C possible=2C even =
with an embed language as PHP is.
++$i is not different=2C from my point of view=2C from a code where each se=
quential push is performed via array_push($arr=2C $value) rather than $arr[=
] =3D $value=3B
Same is for all those loop such
for($i =3D 0=3B $i < count($staticStack)=3B $i++)=3B
for me alien=2C since I've always done
for($i =3D 0=2C $length =3D count($staticStack)=3B $i < $length=3B ++$i)=3B
or=2C even better=2C a core performed loop when I need values
foreach($staticStack as $value)=3B
these are just examples=2C code style=2C whatever you want=2C and I'll neve=
r change my style unless there is a valid reason and some bench able to dem=
onstrate I am wrong. I guess it's just a matter of point of views=2C but I =
cannot suggest slower practice cause Moore said tomorrow that CPU will stri=
ke the millisecond=2C 'cause on micro benchmarks=2C we can go faster=2C and=
that's it.
Regards
=0A=
____________________________________________________________ _____=0A=
Windows Live: Keep your friends up to date with what you do online.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_1:092=
010=
--_29f65477-0bf0-4c8f-96f1-d750f69fa301_--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:06:46 von Andrea Giammarchi
--_e8835d46-9eb5-43a8-90f4-afc36389cb51_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> Furthermore=2C the amount
> of time micro-optimization takes up (going through old code=2C I mean)
> could be better spent doing something that actually does increase your
> performance=2C like implementing a search engine or memcached. Going
> forward=2C if you're aware that ++i and i++ are the same for your
> application and ++i is a single php opcode faster (which I don't know
> if it's even measurable=2C that difference)=2C sure go ahead and use ++i
> but it's certainly not worth serious thought or developer time.
>=20
> My two cents.
I do micro optimization with every language I use=2C when I know=2C and whe=
re I can. I am the one that usually solves slow query problems=2C and I use=
best practices on database as well.
Guys=2C I don't get your point ... if you know that "$var" is a non-sense=
=2C feel free to use it ... what I know=2C is that every double quoted stri=
ng require parsing=2C due to variable or char evaluations (\x00) evaluation=
=2C if I don't need this waste of time=2C why should I write a totally mean=
ingless=2C useless=2C "$var" where $var is sufficient or more over 'whateve=
r'.$var will be faster?
The fact is that this is my approach for every layer of an application=2C I=
am not like that only with PHP. There is something to optimize? Make it yo=
ur code style and you won't spend a sinlge second more than any other=2C bu=
t at least you'll do your best to reach best performances.
As I have said=2C I have never had performances problem=2C and I am a full =
web stack developer=2C but you can obviously do whatever you want=2C is sti=
ll a matter of points of view.
Regards
=0A=
____________________________________________________________ _____=0A=
Windows Live: Keep your friends up to date with what you do online.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_1:092=
010=
--_e8835d46-9eb5-43a8-90f4-afc36389cb51_--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:07:40 von Andrea Giammarchi
--_62700b0a-8e72-4153-9644-cfc10ca17260_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
ah ah ah .... that's for sure=2C I've never said that is correct=2C I said =
that is illogical =3B-)
> Subject: RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$fo=
o
> Date: Tue=2C 6 Oct 2009 12:46:36 -0500
> From: jblanchard@pocket.com
> To: an_red@hotmail.com=3B tedd.sperling@gmail.com=3B php-general@lists.ph=
p.net
>=20
> [snip]
> ...micro optimizations...
> [/snip]
>=20
> And in the land of micro optimization you would likely never see the
> following=3B
>=20
> $a =3D $a++=3B
=0A=
____________________________________________________________ _____=0A=
Windows Live: Keep your friends up to date with what you do online.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_1:092=
010=
--_62700b0a-8e72-4153-9644-cfc10ca17260_--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:09:48 von Andrea Giammarchi
--_cbf8d6e8-adce-4bd3-b32e-e4f18ca83f55_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> Eddie:
>=20
> And thanks for supporting my point.
so you think as well that 3 characters=2C written like this i++=2C in a car=
eless way=2C or like this ++i=2C make the difference about time spent to de=
velop ... interesting
Regards
=0A=
____________________________________________________________ _____=0A=
Windows Live: Keep your friends up to date with what you do online.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_1:092=
010=
--_cbf8d6e8-adce-4bd3-b32e-e4f18ca83f55_--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:12:50 von Andrea Giammarchi
--_368ca406-7895-42bd-a775-d4c830364e3b_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
> Speaking of starting a conversation=2C what do you think about the "goto"
> construct introduced just recently?
if used properly=2C could avoid recursion=2C and speed up operations ... th=
ere is nothing wrong with goto=2C everything we write on lowest level is a =
jump in the memory (as goto is a jump in the code flow)
++goto ... and not goto++
Regards
=0A=
____________________________________________________________ _____=0A=
Windows Live: Make it easier for your friends to see what you=92re up to on=
Facebook.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_2:092=
009=
--_368ca406-7895-42bd-a775-d4c830364e3b_--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:17:44 von Andrea Giammarchi
--_22e749e0-b498-477e-afc8-d6cb32f46c20_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
> if used properly=2C could avoid recursion=2C and speed up operations ... =
there is nothing wrong with goto=2C everything we write on lowest level is =
a jump in the memory (as goto is a jump in the code flow)
>=20
> ++goto ... and not goto++
I forgot=2C I have always used goto in Batch script=2C which indeed can emu=
lates functions=2C except there is no recursion problem. I have a couple of=
batches online if interesting=2C and from performances point of view=2C as=
k yourself why on earth PHP core developers have introduced goto and actual=
ly somebody is using it =3B-)
Uh=2C I forgot I live in microoptimization land ... lol
=0A=
____________________________________________________________ _____=0A=
Keep your friends updated=97even when you=92re not signed in.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_5:092=
010=
--_22e749e0-b498-477e-afc8-d6cb32f46c20_--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:20:02 von Clancy
On Tue, 6 Oct 2009 14:08:14 -0400, tedd.sperling@gmail.com (tedd) wrote:
>At 10:48 AM -0300 10/6/09, Martin Scotta wrote:
>>No matter how silly it can looks like (a = a++) it is still completely valid
>>code and it SHOULD run without problems.
>
>Yeah, it's a valid as:
>
> $a = $a;
>
>and does the same thing, which is nothing.
No; it's worse, because it can be interpreted in two different ways, which is demonstrated
by the fact that it gives different results in different languages.
>If you want a statement that does something, then use:
>
> $a = ++$a;
>
>or simply:
>
> $a++;
>
>or
>
> ++$a;
>
>Any of those will increment $a, whereas ($a = $a++;) does nothing.
According to Schlossnagel "Advanced PHP programming" it is better to use ++$a, because
this simply increments the variable, whereas $a++ makes a copy, and then increments the
variable, so it involves additional time and memory usage. I cannot see that it would ever
make a difference in the real world, but this is one of the tricks Schlossnagel advises
you should use when you want the fastest possible code.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:30:27 von Clancy
On Tue, 6 Oct 2009 12:15:54 -0400, tedd.sperling@gmail.com (tedd) wrote:
>At 3:56 PM +0200 10/6/09, Andrea Giammarchi wrote:
>> > Does these behaves exactly?
>>> for($i=0; $i<10; ++$i)
>>> for($i=0; $i<10; $i++)
>>
>>different benchmarks showed ++$i is usually faster than $i++
>
>"Faster" is a relative term that is becoming more meaningless each year.
>
>Considering that "speed" is increasing and "memory" prices are
>dropping exponentially, both of those are becoming less and less
>important in design considerations (my opinion).
>
>The speeds of the Crays of yesteryear we are now holding in our hands
>as cell phones. The memory we are buying today is literally fractions
>of a cent of the tens of thousands of dollars we spent some 20 years
>ago.
My memory is a bit vague, but I think my first hard disk had 10 MB, and cost about $2000--
about $0.20 a kilobyte. Now you can buy a 1 TB hard disk for less than $100 -- less than
$0.10 a gigabyte.
>I venture to claim the time it took me to write this email (and for
>you to read it) was longer than the total time saved between using
>++$i vs $i++ for all the php scripts in the world over the remaining
>life span of PHP.
>
>Interesting "food for thought", huh?
When I started computing I could get five runs a week if I used the little "local"
computer (with 32K of 24 bit words, and costing $500,000), or three runs a week if I used
the big computer in Canberra (which had four times as much memory and cost $2 million).
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:37:52 von TedD
At 1:09 AM +0200 10/7/09, Andrea Giammarchi wrote:
> > Eddie:
>>
>> And thanks for supporting my point.
>
>so you think as well that 3 characters, written like this i++, in a
>careless way, or like this ++i, make the difference about time spent
>to develop ... interesting
No, just the opposite. It doesn't make any difference either way.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 01:45:01 von TedD
At 10:20 AM +1100 10/7/09, clancy_1@cybec.com.au wrote:
>On Tue, 6 Oct 2009 14:08:14 -0400, tedd.sperling@gmail.com (tedd) wrote:
>
>>At 10:48 AM -0300 10/6/09, Martin Scotta wrote:
>>>No matter how silly it can looks like (a = a++) it is still completely valid
>>>code and it SHOULD run without problems.
>>
>>Yeah, it's a valid as:
>>
>> $a = $a;
>>
>>and does the same thing, which is nothing.
>
>No; it's worse, because it can be interpreted in two different ways,
>which is demonstrated
>by the fact that it gives different results in different languages.
That's true for different languages, but I was talking about php. In
some languages even the variable $a wouldn't be legal.
However, you are correct that the confusion such assignments would
raise would create problems as well.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 03:15:49 von Daevid Vincent
> -----Original Message-----
> From: Paul M Foster [mailto:paulf@quillandmouse.com]
> Sent: Tuesday, October 06, 2009 6:28 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Whacky increment/assignment logic with
> $foo++ vs ++$foo
>
> On Tue, Oct 06, 2009 at 08:51:17AM -0400, Bob McConnell wrote:
>
> > From: Joost [mailto:joost.t.hart@planet.nl]
> > > "Daevid Vincent" wrote:
> > >>> From: Ben Dunlap [mailto:bdunlap@agentintellect.com]
> >
> > > $a = $a++;
> >
> > I just think this is an ambiguous line of code that wasn't thought
> > through. The presence of the postfix operator makes the result
> > undefined, no matter what language you are using. It will
> be an accident
> > if you get the results you are expecting.
>
> The behavior of the ++ operator is the invention of Kernighan
> and Ritchie.
> I don't imagine they ever foresaw anyone doing something as silly as
>
> a = a++;
>
> except under the rarest of circumstances.
>
> Paul
Except that:
$a = 123;
$b = $a++;
echo $b; //gives 123, not 124
as you logically expect it to and common sense would dictate, regardless of
what K&R or anyone else says.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 03:20:37 von Daevid Vincent
HEY! Don't try to hijack my astonishingly long-running thread! Start your
own Jay. ;-}
> -----Original Message-----
> From: Jay Blanchard [mailto:jblanchard@pocket.com]
> Sent: Tuesday, October 06, 2009 11:34 AM
> To: Tommy Pham; php-general@lists.php.net
> Subject: RE: [PHP] Whacky increment/assignment logic with
> $foo++ vs ++$foo
>
> [snip]
> I find it interesting for a discussion to go on this long for
> something
> as
> $a = $a++;
> [/snip]
>
> You think that is interesting? Start a conversation about these
>
> {}
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 04:20:16 von Robert Cummings
Andrea Giammarchi wrote:
>
>
>> if used properly, could avoid recursion, and speed up operations ... there is nothing wrong with goto, everything we write on lowest level is a jump in the memory (as goto is a jump in the code flow)
>>
>> ++goto ... and not goto++
>
> I forgot, I have always used goto in Batch script, which indeed can emulates functions, except there is no recursion problem. I have a couple of batches online if interesting, and from performances point of view, ask yourself why on earth PHP core developers have introduced goto and actually somebody is using it ;-)
>
> Uh, I forgot I live in microoptimization land ... lol
If you read the archives for PHP Internals you can view the discussion
that went into the final decision to include GOTO. It happened 2 or 3
years ago and I was certainly on the side arguing in its favour. The
GOTO used in languages such as C and now PHP, is not the bastard GOTO of
BASIC yesteryear. There are certainly use cases where the use of GOTO
makes far more sense than other constructs. Finite state machines (often
used in parsing) are one such place. In fact if you grep for goto on any
major open source project's C code, you will probably find multiple
occurrences. PHP, MySQL, and Apache all use goto.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 14:22:54 von Jay Blanchard
Speaking of.....
[snip]
-----Original Message-----
From: Daevid Vincent [mailto:daevid@daevid.com]=20
Sent: Tuesday, October 06, 2009 8:21 PM
To: Jay Blanchard; 'Tommy Pham'; php-general@lists.php.net
Subject: RE: [PHP] Whacky increment/assignment logic with $foo++ vs
++$foo
HEY! Don't try to hijack my astonishingly long-running thread! Start
your
own Jay. ;-}=20
> -----Original Message-----
> From: Jay Blanchard [mailto:jblanchard@pocket.com]=20
> Sent: Tuesday, October 06, 2009 11:34 AM
> To: Tommy Pham; php-general@lists.php.net
> Subject: RE: [PHP] Whacky increment/assignment logic with=20
> $foo++ vs ++$foo
>=20
> [snip]
> I find it interesting for a discussion to go on this long for=20
> something
> as
> $a =3D $a++;
> [/snip]
>=20
> You think that is interesting? Start a conversation about these=20
>=20
> {}
>=20
> --=20
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>=20
[/snip]
....things that start long discussions! :)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 14:54:35 von TedD
At 6:15 PM -0700 10/6/09, Daevid Vincent wrote:
>Except that:
>
>$a = 123;
>$b = $a++;
>echo $b; //gives 123, not 124
>
>as you logically expect it to and common sense would dictate, regardless of
>what K&R or anyone else says.
That's not the way I look at it.
$b = $a++;
means to me "take the value of $a and assign to $b and then increment $a."
Whereas:
$b = ++$a;
means to me "increment $a and take the value of $a and assign to $b."
Cheers,
tedd
--
-----
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 14:59:23 von Ashley Sheridan
--=-pkCWJB3ko36aJ90oWCh7
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Wed, 2009-10-07 at 08:54 -0400, tedd wrote:
> At 6:15 PM -0700 10/6/09, Daevid Vincent wrote:
> >Except that:
> >
> >$a = 123;
> >$b = $a++;
> >echo $b; //gives 123, not 124
> >
> >as you logically expect it to and common sense would dictate, regardless of
> >what K&R or anyone else says.
>
> That's not the way I look at it.
>
> $b = $a++;
>
> means to me "take the value of $a and assign to $b and then increment $a."
>
> Whereas:
>
> $b = ++$a;
>
> means to me "increment $a and take the value of $a and assign to $b."
>
> Cheers,
>
> tedd
>
> --
> -----
> http://sperling.com http://ancientstones.com http://earthstones.com
>
Which is exactly the reason for the two operators in C.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-pkCWJB3ko36aJ90oWCh7--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 16:46:52 von Eddie Drapkin
On Tue, Oct 6, 2009 at 6:59 PM, Andrea Giammarchi wrot=
e:
>
>
>> So while we can debate computing considerations of today, tomorrow
>> those will be less important. That was the point I was making. Why
>> not focus on things that make significant difference and let the
>> insignificant fade into history.
>
> I tendentiously focus on all things able to make, all together, even more=
significant difference.
>
> Some micro-optimization, used as common code style, can make the entire a=
pplication or the specific performance critical task, possible, even with a=
n embed language as PHP is.
>
> ++$i is not different, from my point of view, from a code where each sequ=
ential push is performed via array_push($arr, $value) rather than $arr[] =
=3D $value;
> Same is for all those loop such
> for($i =3D 0; $i < count($staticStack); $i++);
>
> for me alien, since I've always done
>
> for($i =3D 0, $length =3D count($staticStack); $i < $length; ++$i);
>
> or, even better, a core performed loop when I need values
> foreach($staticStack as $value);
>
> these are just examples, code style, whatever you want, and I'll never ch=
ange my style unless there is a valid reason and some bench able to demonst=
rate I am wrong. I guess it's just a matter of point of views, but I cannot=
suggest slower practice cause Moore said tomorrow that CPU will strike the=
millisecond, 'cause on micro benchmarks, we can go faster, and that's it.
>
> Regards
>
> ____________________________________________________________ _____
> Windows Live: Keep your friends up to date with what you do online.
> http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/=
social-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL: en-xm:SI_SB_1:0=
92010
And thus we have ANOTHER problem with PHP micro-optimizations; the
proponents of using them are often misinformed or downright wrong
about them. Two things you've mentioned in this thread, your count
method and using string concatenation vs. string interpolation (' v.
") what you've proposed is actually slower!
To wit:
I can write a test[1] that comes out with these results:
String concat time: 0.18807196617126
String interpolation time: 0.14288902282715
Where using " is faster than ' ! Common wisdom be damned!
Similarly another test [2] shows that your count() method is less than
microseconds faster per iteration. Note that these tests are actually
run by codepad.org so I have no opportunity to fudge the results.
[1] http://codepad.org/J2PRycef
[2] http://codepad.org/7jFK9HHY
The amount of time you spend making your code less readable and
thinking about for microoptimizations, even if they are a habit, is
going to be entirely wasted when your micro-optimization habits are
rooted in falsehoods.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 17:35:05 von Andrea Giammarchi
--_9955ba72-ecad-4322-aa36-81ed511f22f4_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
>=20
> I can write a test[1] that comes out with these results:
> String concat time: 0.18807196617126
> String interpolation time: 0.14288902282715
> Where using " is faster than ' ! Common wisdom be damned!
where is the test? ... and=2C is that kind of test where you put 12345678 v=
ariables inside a string?
Cause I am developer=2C not a monkey=2C obviously if there are more than TO=
T concatenations to unclude variables I won't use string concatenation ... =
now test my case=2C the string $var instead of "$var"=2C and come back when=
ever you want.
> Similarly another test [2] shows that your count() method is less than
> microseconds faster
that's enough for me ... because I don't write 1 line of code=2C but thousa=
nds of lines of code for an application.
Less than a millisecond for a single loop=2C means a second when there are =
a lot of loops.
Micro optimizations=2C as I said=2C are simply faster ... and I don't waste=
any time=2C I simply know what could be better=2C for that circumstance=2C=
and I use it. Waste of time is only if you don't know micro optimizations=
=2C to me these are natural=2C we write same application in the same time=
=2C trust me=2C except mine will have micro optimizations ... and at the en=
d same development time=2C faster result for me=2C it's 1 plus 1
For those without micro optimizations knowledge=2C here there is a good sta=
rt point but there is more:
http://www.alexatnet.com/node/196
Regards=2C still waiting for a real case scenario where my code style will =
be slower ( the array =2C as $arr[] is the fastest=2C compare with a functi=
on call=2C array_push=2C and bring me results if you have time=2C but THAT =
will be a waste of time )
=0A=
____________________________________________________________ _____=0A=
Windows Live: Friends get your Flickr=2C Yelp=2C and Digg updates when they=
e-mail you.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_3:092=
010=
--_9955ba72-ecad-4322-aa36-81ed511f22f4_--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 17:36:27 von Tom Worster
just yesterday i was reading through this wonderful and very funny
presentation:
http://talks.php.net/show/froscon08/0
for me it really drove home the message (among others) that it makes sense
to find out where the real gains can be made before investing your efforts
in optimization.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 17:50:14 von Andrea Giammarchi
--_2d358216-4212-4d85-a067-750833820069_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I don't get why for you code style means effort=2C waste of time ... I bloo=
dy write code=2C how do you write a loop?
The same as I do ... except I put ++i rather than i++ ... does it change AN=
YTHING?
For you no=2C for me yes ... whre is the drama here? I cannot spot it=2C it=
's like saying: I don't know optimizations and for this reason I blame them=
... any sense from a development point of view? Absolutely no=2C imho=2C 1=
0 years over PHP I know this stuff=2C it's my code style ... zero problems=
=2C zero effort=2C zero waste of time ... maybe it could be for you ... but=
if you think ++$i =2C which is natural for me=2C is gonna take all that ef=
fort=2C well=2C ... I don't want to comment it ...
It's just a flame now=2C you are acting like I am spending my life to micro=
optimize PHP which is simply a silly opinion of what I have said=2C I just=
code like that ... and I have never had problems. You neither? Good for yo=
u=2C I have something else to think about=2C cause micro optimizations are =
not something to think about for me=2C I just code like that ... maybe if I=
repeat again myself you'll get the point ...
I just code like that=2C and until you'll prove my code is bad=2C which is =
not gonna happen since optimizations for you are a waste of time=2C I'll co=
de like that.
Have fun=2C I have wasted already too much time to taqlk about this silly s=
tuff ...
Best Regards
> Date: Wed=2C 7 Oct 2009 11:36:27 -0400
> Subject: Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$fo=
o
> From: fsb@thefsb.org
> To: oorza2k5@gmail.com=3B an_red@hotmail.com
> CC: tedd.sperling@gmail.com=3B php-general@lists.php.net
>=20
> just yesterday i was reading through this wonderful and very funny
> presentation:
>=20
> http://talks.php.net/show/froscon08/0
>=20
> for me it really drove home the message (among others) that it makes sens=
e
> to find out where the real gains can be made before investing your effort=
s
> in optimization.
>=20
>=20
=0A=
____________________________________________________________ _____=0A=
Windows Live Hotmail: Your friends can get your Facebook updates=2C right f=
rom Hotmail=AE.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_4:092=
009=
--_2d358216-4212-4d85-a067-750833820069_--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 17:54:15 von Eddie Drapkin
On Wed, Oct 7, 2009 at 11:36 AM, Tom Worster wrote:
> just yesterday i was reading through this wonderful and very funny
> presentation:
>
> Â Â http://talks.php.net/show/froscon08/0
>
> for me it really drove home the message (among others) that it makes sens=
e
> to find out where the real gains can be made before investing your effort=
s
> in optimization.
>
>
>
This is a great talk / slideshow and definitely is a better way to
drive home the point that PHP execution speed is relatively
meaningless in terms of user experience. Well, at least up to a
point...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 18:00:37 von Andrea Giammarchi
--_38d085fa-a3dc-4cb6-b53b-ecdf928ae073_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> This is a great talk / slideshow and definitely is a better way to
> drive home the point that PHP execution speed is relatively
> meaningless in terms of user experience. Well=2C at least up to a
> point...
if it takes 0.1 per response=2C with 10 users will be 1 second to wait ... =
if it takes 0.02 per user=2C you can manage much more users.
It is that simple=2C and thee reason there is APC=2C other op cache manager=
s/optimizers=2C is that speed is always critical for high traffic websites/=
services ... and if there is a possibility to help=2C the same way you just=
ify APC=2C since PHP is not the bottleneck=2C I don't get why you are blami=
ng me to use best practices ... seriously=2C what's wrong with me using ++$=
i it's absolutely a mystery!!!
I guess you develop without caring about INSERT DELAYED when necessary=2C m=
ysql_fetch_row when you know the structure=2C rather than mysql_fetch_assoc=
=2C and everything else that could preserve half a second ... Google server=
s truncated layout to make everything faster=2C I can only smile thinking a=
bout your arguments in a Google interview ... "who care about optimizations=
=2C I am already loads of crap due to slow queries" ... well=2C it's your c=
hoice=2C and not my fault.
Regards ... and thanks for the slide
=0A=
____________________________________________________________ _____=0A=
Windows Live Hotmail: Your friends can get your Facebook updates=2C right f=
rom Hotmail=AE.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_4:092=
009=
--_38d085fa-a3dc-4cb6-b53b-ecdf928ae073_--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 18:07:01 von Jay Blanchard
[snip]
....flame...
[/snip]
Easy there hoss, no need to get worked up.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 18:09:45 von Andrea Giammarchi
--_e4d5f24c-de23-4447-94e4-272ceaa92997_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> Easy there hoss=2C no need to get worked up.
In my opinion=2C being blamed for natural optimizations is the most ridicul=
ous=2C hilarious=2C anti professional behavior I have ever seen ... but you=
are right=2C no need to get worked up=2C so have fun here.
Regards
=0A=
____________________________________________________________ _____=0A=
Windows Live Hotmail: Your friends can get your Facebook updates=2C right f=
rom Hotmail=AE.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see- it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en -xm:SI_SB_4:092=
009=
--_e4d5f24c-de23-4447-94e4-272ceaa92997_--
RE: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 21:42:41 von TedD
At 1:59 PM +0100 10/7/09, Ashley Sheridan wrote:
>>On Wed, 2009-10-07 at 08:54 -0400, tedd wrote:
>>At 6:15 PM -0700 10/6/09, Daevid Vincent wrote:
>>>Except that:
>>>
>>>$a = 123;
>>>$b = $a++;
>>>echo $b; //gives 123, not 124
>>>
>>>as you logically expect it to and common sense would dictate, regardless of
>>>what K&R or anyone else says.
>>
>>That's not the way I look at it.
>>
>> $b = $a++;
>>
>>means to me "take the value of $a and assign to $b and then increment $a."
>>
>>Whereas:
>>
>> $b = ++$a;
>>
>>means to me "increment $a and take the value of $a and assign to $b."
>>
>>Cheers,
>>
>>tedd
>>
>>
>
>Which is exactly the reason for the two operators in C.
>
>Thanks,
>Ash
Ash:
The reason was simply to provide a different way of doing something.
For example, take the statements of:
$a = 10;
$b = a$++; // $b = 10 and $a = 11
This post-increment operator was a way to assign 10 to $b and
increment $a in one statement.
Whereas:
$a = 10;
$b = ++a$; // $b = 11 and $a = 11
This pre-increment operator was a way to increment $a and also assign
that value to $b.
Both are perfectly valid ways of using the operator. Also realize
that the pre-decrement and post-decrement operators worked in similar
fashion.
Now why would someone want to do that? There could be many reasons,
but that was left to the programmer to use as he/she needed.
However, what I find wacky about all of this is:
for($i=1; $i<=10; $i++)
{
echo($i);
}
and
for($i=1; $i<=10; ++$i)
{
echo($i);
}
Do exactly the same thing. I would have expected the first to print
1-10, while the second to print 2-10, but they both print 1-10.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 21:58:41 von Andrew Ballard
--000e0cd4866c0d67a004755dc64b
Content-Type: text/plain; charset=UTF-8
On Wed, Oct 7, 2009 at 3:42 PM, tedd wrote:
> However, what I find wacky about all of this is:
>
> for($i=1; $i<=10; $i++)
> {
> echo($i);
> }
>
> and
>
> for($i=1; $i<=10; ++$i)
> {
> echo($i);
> }
>
> Do exactly the same thing. I would have expected the first to print 1-10,
> while the second to print 2-10, but they both print 1-10.
>
> Cheers,
>
> tedd
>
>
Not wacky at all. Think of them both this way:
$i = 1;
while ($i <= 10) {
$i++;
echo ($i);
}
// or
$i = 1;
while ($i <= 10) {
++$i;
echo ($i);
}
?>
Andrew
--000e0cd4866c0d67a004755dc64b--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 22:00:03 von Andrew Ballard
--000e0cd51498e67b9904755dcaee
Content-Type: text/plain; charset=UTF-8
On Wed, Oct 7, 2009 at 3:58 PM, Andrew Ballard wrote:
> On Wed, Oct 7, 2009 at 3:42 PM, tedd wrote:
>
>> However, what I find wacky about all of this is:
>>
>> for($i=1; $i<=10; $i++)
>> {
>> echo($i);
>> }
>>
>> and
>>
>> for($i=1; $i<=10; ++$i)
>> {
>> echo($i);
>> }
>>
>> Do exactly the same thing. I would have expected the first to print 1-10,
>> while the second to print 2-10, but they both print 1-10.
>>
>> Cheers,
>>
>> tedd
>>
>>
> Not wacky at all. Think of them both this way:
>
>
> $i = 1;
> while ($i <= 10) {
> $i++;
> echo ($i);
> }
>
> // or
>
> $i = 1;
> while ($i <= 10) {
> ++$i;
> echo ($i);
> }
>
> ?>
>
> Andrew
>
oops. Flip the lines inside the while, but you get the idea.
$i = 1;
while ($i <= 10) {
echo $i;
$i++;
}
Andrew
--000e0cd51498e67b9904755dcaee--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 07.10.2009 22:24:29 von Tommy Pham
--0-1313413490-1254947069=:68424
Content-Type: text/plain; charset=us-ascii
________________________________
From: tedd
To: php-general@lists.php.net; ash@ashleysheridan.co.uk; Daevid Vincent
Sent: Wed, October 7, 2009 12:42:41 PM
Subject: RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
At 1:59 PM +0100 10/7/09, Ashley Sheridan wrote:
>>On Wed, 2009-10-07 at 08:54 -0400, tedd wrote:
>>At 6:15 PM -0700 10/6/09, Daevid Vincent wrote:
>>>Except that:
>>>
>>>$a = 123;
>>>$b = $a++;
>>>echo $b; //gives 123, not 124
>>>
>>>as you logically expect it to and common sense would dictate, regardless of
>>>what K&R or anyone else says.
>>
>>That's not the way I look at it.
>>
>> $b = $a++;
>>
>>means to me "take the value of $a and assign to $b and then increment $a."
>>
>>Whereas:
>>
>> $b = ++$a;
>>
>>means to me "increment $a and take the value of $a and assign to $b."
>>
>>Cheers,
>>
>>tedd
>>
>>
>
>Which is exactly the reason for the two operators in C.
>
>Thanks,
>Ash
Ash:
The reason was simply to provide a different way of doing something.
For example, take the statements of:
$a = 10;
$b = a$++; // $b = 10 and $a = 11
This post-increment operator was a way to assign 10 to $b and
increment $a in one statement.
Whereas:
$a = 10;
$b = ++a$; // $b = 11 and $a = 11
This pre-increment operator was a way to increment $a and also assign
that value to $b.
Both are perfectly valid ways of using the operator. Also realize
that the pre-decrement and post-decrement operators worked in similar
fashion.
Now why would someone want to do that? There could be many reasons,
but that was left to the programmer to use as he/she needed.
However, what I find wacky about all of this is:
for($i=1; $i<=10; $i++)
{
echo($i);
}
and
for($i=1; $i<=10; ++$i)
{
echo($i);
}
Do exactly the same thing. I would have expected the first to print
1-10, while the second to print 2-10, but they both print 1-10.
Cheers,
tedd
Tommy>> Why would expect to print 2-10? The way I read the for loop is: start $i with 1, do loop body until $i <= 10, increment $i before next loop. So whether post/pre-increment doesn't matter logically. Moreover, your loop can also be written as:
for ($i=1; $i <= 10;)
{
echo ($i++);
}
PS: I hate to send reply in 'rich text' but Yahoo's plain text screw up the quote... I think it's time to switch over to gmail...
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--0-1313413490-1254947069=:68424--
Re: Whacky increment/assignment logic with $foo++ vs ++$foo
am 08.10.2009 11:37:50 von Ashley Sheridan
--=-v9VCD7aX2Cv87TP+ZDMK
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Wed, 2009-10-07 at 13:24 -0700, Tommy Pham wrote:
>
>
> ________________________________
> From: tedd
> To: php-general@lists.php.net; ash@ashleysheridan.co.uk; Daevid Vincent
> Sent: Wed, October 7, 2009 12:42:41 PM
> Subject: RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
>
> At 1:59 PM +0100 10/7/09, Ashley Sheridan wrote:
> >>On Wed, 2009-10-07 at 08:54 -0400, tedd wrote:
> >>At 6:15 PM -0700 10/6/09, Daevid Vincent wrote:
> >>>Except that:
> >>>
> >>>$a = 123;
> >>>$b = $a++;
> >>>echo $b; //gives 123, not 124
> >>>
> >>>as you logically expect it to and common sense would dictate, regardless of
> >>>what K&R or anyone else says.
> >>
> >>That's not the way I look at it.
> >>
> >> $b = $a++;
> >>
> >>means to me "take the value of $a and assign to $b and then increment $a."
> >>
> >>Whereas:
> >>
> >> $b = ++$a;
> >>
> >>means to me "increment $a and take the value of $a and assign to $b."
> >>
> >>Cheers,
> >>
> >>tedd
> >>
> >>
> >
> >Which is exactly the reason for the two operators in C.
> >
> >Thanks,
> >Ash
>
> Ash:
>
> The reason was simply to provide a different way of doing something.
> For example, take the statements of:
>
> $a = 10;
> $b = a$++; // $b = 10 and $a = 11
>
> This post-increment operator was a way to assign 10 to $b and
> increment $a in one statement.
>
> Whereas:
>
> $a = 10;
> $b = ++a$; // $b = 11 and $a = 11
>
> This pre-increment operator was a way to increment $a and also assign
> that value to $b.
>
> Both are perfectly valid ways of using the operator. Also realize
> that the pre-decrement and post-decrement operators worked in similar
> fashion.
>
> Now why would someone want to do that? There could be many reasons,
> but that was left to the programmer to use as he/she needed.
>
> However, what I find wacky about all of this is:
>
> for($i=1; $i<=10; $i++)
> {
> echo($i);
> }
>
> and
>
> for($i=1; $i<=10; ++$i)
> {
> echo($i);
> }
>
>
> Do exactly the same thing. I would have expected the first to print
> 1-10, while the second to print 2-10, but they both print 1-10.
>
> Cheers,
>
> tedd
>
>
> Tommy>> Why would expect to print 2-10? The way I read the for loop is: start $i with 1, do loop body until $i <= 10, increment $i before next loop. So whether post/pre-increment doesn't matter logically. Moreover, your loop can also be written as:
>
> for ($i=1; $i <= 10;)
> {
> echo ($i++);
> }
>
> PS: I hate to send reply in 'rich text' but Yahoo's plain text screw up the quote... I think it's time to switch over to gmail...
>
>
> --
> -------
> http://sperling.com http://ancientstones.com http://earthstones.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
That what I thought I said when I said "Which is exactly the reason for
the two operators in C."
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-v9VCD7aX2Cv87TP+ZDMK--