Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Wed, 2009-10-07 at 13:24 -0700, Tommy Pham wrote: From: tedd tedd.sperl...@gmail.com To: php-general@lists.php.net; a...@ashleysheridan.co.uk; Daevid Vincent dae...@daevid.com 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 KR 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
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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 application or the specific performance critical task, possible, even with an embed language as PHP is. ++$i is not different, from my point of view, from a code where each sequential push is performed via array_push($arr, $value) rather than $arr[] = $value; Same is for all those loop such for($i = 0; $i count($staticStack); $i++); for me alien, since I've always done for($i = 0, $length = 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 change my style unless there is a valid reason and some bench able to demonstrate 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=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_1:092010
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Tue, 6 Oct 2009 12:15:54 -0400, tedd.sperl...@gmail.com (tedd) wrote: At 3:56 PM +0200 10/6/09, Andrea Giammarchi wrote: Does these behaves exactly? for($i=0; $i10; ++$i) for($i=0; $i10; $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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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 KR 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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 KR 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
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
Speaking of. [snip] -Original Message- From: Daevid Vincent [mailto:dae...@daevid.com] 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. ;-} -Original Message- From: Jay Blanchard [mailto:jblanch...@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 [/snip] ...things that start long discussions! :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Tue, Oct 6, 2009 at 6:59 PM, Andrea Giammarchi an_...@hotmail.com wrote: 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 application or the specific performance critical task, possible, even with an embed language as PHP is. ++$i is not different, from my point of view, from a code where each sequential push is performed via array_push($arr, $value) rather than $arr[] = $value; Same is for all those loop such for($i = 0; $i count($staticStack); $i++); for me alien, since I've always done for($i = 0, $length = 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 change my style unless there is a valid reason and some bench able to demonstrate 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=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_1:092010 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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, is that kind of test where you put 12345678 variables inside a string? Cause I am developer, not a monkey, obviously if there are more than TOT concatenations to unclude variables I won't use string concatenation ... now test my case, the string $var instead of $var, and come back whenever 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, but thousands of lines of code for an application. Less than a millisecond for a single loop, means a second when there are a lot of loops. Micro optimizations, as I said, are simply faster ... and I don't waste any time, I simply know what could be better, for that circumstance, and I use it. Waste of time is only if you don't know micro optimizations, to me these are natural, we write same application in the same time, trust me, except mine will have micro optimizations ... and at the end same development time, faster result for me, it's 1 plus 1 For those without micro optimizations knowledge, here there is a good start point but there is more: http://www.alexatnet.com/node/196 Regards, still waiting for a real case scenario where my code style will be slower ( the array , as $arr[] is the fastest, compare with a function call, array_push, and bring me results if you have time, but THAT will be a waste of time ) _ Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_3:092010
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
I don't get why for you code style means effort, waste of time ... I bloody write code, how do you write a loop? The same as I do ... except I put ++i rather than i++ ... does it change ANYTHING? For you no, for me yes ... whre is the drama here? I cannot spot it, 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, imho, 10 years over PHP I know this stuff, it's my code style ... zero problems, zero effort, zero waste of time ... maybe it could be for you ... but if you think ++$i , which is natural for me, is gonna take all that effort, well, ... I don't want to comment it ... It's just a flame now, you are acting like I am spending my life to micro optimize PHP which is simply a silly opinion of what I have said, I just code like that ... and I have never had problems. You neither? Good for you, I have something else to think about, cause micro optimizations are not something to think about for me, I just code like that ... maybe if I repeat again myself you'll get the point ... I just code like that, and until you'll prove my code is bad, which is not gonna happen since optimizations for you are a waste of time, I'll code like that. Have fun, I have wasted already too much time to taqlk about this silly stuff ... Best Regards Date: Wed, 7 Oct 2009 11:36:27 -0400 Subject: Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo From: f...@thefsb.org To: oorza...@gmail.com; an_...@hotmail.com CC: tedd.sperl...@gmail.com; php-general@lists.php.net 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. _ Windows Live Hotmail: Your friends can get your Facebook updates, right from Hotmail®. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Wed, Oct 7, 2009 at 11:36 AM, Tom Worster f...@thefsb.org 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 sense to find out where the real gains can be made before investing your efforts 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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... if it takes 0.1 per response, with 10 users will be 1 second to wait ... if it takes 0.02 per user, you can manage much more users. It is that simple, and thee reason there is APC, other op cache managers/optimizers, is that speed is always critical for high traffic websites/services ... and if there is a possibility to help, the same way you justify APC, since PHP is not the bottleneck, I don't get why you are blaming me to use best practices ... seriously, what's wrong with me using ++$i it's absolutely a mystery!!! I guess you develop without caring about INSERT DELAYED when necessary, mysql_fetch_row when you know the structure, rather than mysql_fetch_assoc, and everything else that could preserve half a second ... Google servers truncated layout to make everything faster, I can only smile thinking about your arguments in a Google interview ... who care about optimizations, I am already loads of crap due to slow queries ... well, it's your choice, and not my fault. Regards ... and thanks for the slide _ Windows Live Hotmail: Your friends can get your Facebook updates, right from Hotmail®. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
[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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
Easy there hoss, no need to get worked up. In my opinion, being blamed for natural optimizations is the most ridiculous, hilarious, anti professional behavior I have ever seen ... but you are right, no need to get worked up, so have fun here. Regards _ Windows Live Hotmail: Your friends can get your Facebook updates, right from Hotmail®. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009
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 KR 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Wed, Oct 7, 2009 at 3:42 PM, tedd tedd.sperl...@gmail.com 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: ?php $i = 1; while ($i = 10) { $i++; echo ($i); } // or $i = 1; while ($i = 10) { ++$i; echo ($i); } ? Andrew
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Wed, Oct 7, 2009 at 3:58 PM, Andrew Ballard aball...@gmail.com wrote: On Wed, Oct 7, 2009 at 3:42 PM, tedd tedd.sperl...@gmail.com 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: ?php $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
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
From: tedd tedd.sperl...@gmail.com To: php-general@lists.php.net; a...@ashleysheridan.co.uk; Daevid Vincent dae...@daevid.com 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 KR 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
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
From: Joost [mailto:joost.t.h...@planet.nl] Daevid Vincent wrote: From: Ben Dunlap [mailto:bdun...@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. Bob McConnell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
It will be an accident if you get the results you are expecting. I agree that the operation is illogical, I must disagree about accidents. In PHP that operation will mean assign to the new $a variable the value returned from the other $a variable before the increment. There is no mystery here, imho. 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=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_1:092010
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.h...@planet.nl] Daevid Vincent wrote: From: Ben Dunlap [mailto:bdun...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Tue, Oct 6, 2009 at 10:28 AM, Paul M Foster pa...@quillandmouse.comwrote: On Tue, Oct 06, 2009 at 08:51:17AM -0400, Bob McConnell wrote: From: Joost [mailto:joost.t.h...@planet.nl] Daevid Vincent wrote: From: Ben Dunlap [mailto:bdun...@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; $i10; ++$i) for($i=0; $i10; $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
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
Does these behaves exactly? for($i=0; $i10; ++$i) for($i=0; $i10; $i++) different benchmarks showed ++$i is usually faster than $i++ In that loop case, yes, what's happen internally is exactly the same, $i will be from 0 to 9, 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, it's pretty simple, as is that operation, in PHP. In other languages, could have been the same, rarely in scripting languages though, at least those with still primitive scalar values (int, float, string, bool) Regards _ Windows Live Hotmail: Your friends can get your Facebook updates, right from Hotmail®. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
At 3:56 PM +0200 10/6/09, Andrea Giammarchi wrote: Does these behaves exactly? for($i=0; $i10; ++$i) for($i=0; $i10; $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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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. Regards Date: Tue, 6 Oct 2009 12:15:54 -0400 To: php-general@lists.php.net From: tedd.sperl...@gmail.com Subject: RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo At 3:56 PM +0200 10/6/09, Andrea Giammarchi wrote: Does these behaves exactly? for($i=0; $i10; ++$i) for($i=0; $i10; $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 _ Windows Live: Make it easier for your friends to see what you’re up to on Facebook. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_2:092009
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
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
- Original Message From: tedd tedd.sperl...@gmail.com To: Martin Scotta martinsco...@gmail.com; Paul M Foster pa...@quillandmouse.com 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [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
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Tue, Oct 6, 2009 at 2:34 PM, Jay Blanchard jblanch...@pocket.com 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.
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Tue, Oct 6, 2009 at 14:46, Israel Ekpo israele...@gmail.com 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 P. Brown daniel.br...@parasane.net || danbr...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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. I do micro optimization with every language I use, when I know, and where I can. I am the one that usually solves slow query problems, and I use best practices on database as well. Guys, I don't get your point ... if you know that $var is a non-sense, feel free to use it ... what I know, is that every double quoted string require parsing, due to variable or char evaluations (\x00) evaluation, if I don't need this waste of time, why should I write a totally meaningless, useless, $var where $var is sufficient or more over 'whatever'.$var will be faster? The fact is that this is my approach for every layer of an application, I am not like that only with PHP. There is something to optimize? Make it your code style and you won't spend a sinlge second more than any other, but at least you'll do your best to reach best performances. As I have said, I have never had performances problem, and I am a full web stack developer, but you can obviously do whatever you want, is still a matter of points of view. 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=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_1:092010
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
ah ah ah that's for sure, I've never said that is correct, I said that is illogical ;-) Subject: RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo Date: Tue, 6 Oct 2009 12:46:36 -0500 From: jblanch...@pocket.com To: an_...@hotmail.com; tedd.sperl...@gmail.com; php-general@lists.php.net [snip] ...micro optimizations... [/snip] And in the land of micro optimization you would likely never see the following; $a = $a++; _ 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=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_1:092010
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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 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=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_1:092010
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
Speaking of starting a conversation, what do you think about the goto construct introduced just recently? 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++ Regards _ Windows Live: Make it easier for your friends to see what you’re up to on Facebook. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_2:092009
RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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 _ Keep your friends updated—even when you’re not signed in. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_5:092010
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Tue, 6 Oct 2009 14:08:14 -0400, tedd.sperl...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
At 10:20 AM +1100 10/7/09, clanc...@cybec.com.au wrote: On Tue, 6 Oct 2009 14:08:14 -0400, tedd.sperl...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
-Original Message- From: Paul M Foster [mailto:pa...@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.h...@planet.nl] Daevid Vincent wrote: From: Ben Dunlap [mailto:bdun...@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 KR or anyone else says. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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. ;-} -Original Message- From: Jay Blanchard [mailto:jblanch...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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_def...@yahoo.de Ben Dunlap bdun...@agentintellect.com wrote in message news:7997e80e0910021458h20ebd75dtfc51f9264f351...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
At 5:12 PM -0400 10/2/09, Robert Cummings wrote: Daevid Vincent wrote: ?PHP $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: ?php $index = 1; foreach( @items as @item ) { echo 'tr' .'td'.($index++).'/td' .'td'.$item.'/td' .'/tr'; } ? 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: ?php $index = 0; foreach( @items as @item ) { echo 'tr' .'td'.(++$index).'/td' .'td'.$item.'/td' .'/tr'; } ? 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Sat, Oct 3, 2009 at 10:49, tedd tedd.sperl...@gmail.com 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 P. Brown daniel.br...@parasane.net || danbr...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
... 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 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 = 2, b; b = a++; printf(b = [%d]\n, b); and b will be exactly 2. In PHP you are not referencing that variable, you are overwriting variable $a with an integer, 2, and that's it. The incremented integer, 3, is simply lost in the silly logic of the operation. The equivalent of your C code, in PHP, would be just this: $a = 2; $a++; print $a; // of course is 3, the initial $a is not lost or, to be more explicit ... $a = 2; ($a = $a) and $a++; print $a; Regards _ Keep your friends updated—even when you’re not signed in. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_5:092010
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [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
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
Daevid Vincent wrote: ?PHP $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: ?php $index = 1; foreach( @items as @item ) { echo 'tr' .'td'.($index++).'/td' .'td'.$item.'/td' .'/tr'; } ? 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: ?php $index = 0; foreach( @items as @item ) { echo 'tr' .'td'.(++$index).'/td' .'td'.$item.'/td' .'/tr'; } ? 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
- Original Message From: Ben Dunlap bdun...@agentintellect.com To: Daevid Vincent dae...@daevid.com 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
-Original Message- From: Robert Cummings [mailto:rob...@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: ?PHP $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: ?php $index = 1; foreach( @items as @item ) { echo 'tr' .'td'.($index++).'/td' .'td'.$item.'/td' .'/tr'; } ? 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: ?php $index = 0; foreach( @items as @item ) { echo 'tr' .'td'.(++$index).'/td' .'td'.$item.'/td' .'/tr'; } ? 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Fri, Oct 2, 2009 at 17:28, Daevid Vincent dae...@daevid.com 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: ?php $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: ?php $a = array('foo' = 'bar', 'apple' = 'red', 'lime' = 'green', 'wife' = 'pain in ass', 'lemon' = 'yellow'); for($i=0;$icount($a);$i++) { // } ? but rather by something just slightly more advanced: ?php $num = file_get_contents('visitcount.txt'); if(isset($_GET['countme'])) { echo You are visitor #.++$num.br /\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 P. Brown daniel.br...@parasane.net || danbr...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Fri, Oct 2, 2009 at 17:42, Daniel Brown danbr...@php.net wrote: but rather by something just slightly more advanced: ?php $num = file_get_contents('visitcount.txt'); if(isset($_GET['countme'])) { echo You are visitor #.++$num.br /\n; file_put_contents($num); } ? Converse example (sorry, forgot to add it): ?php $num = file_get_contents('visitcount.txt'); if(isset($_GET['countme'])) { echo There were .$num++. people here before you.br /\n; file_put_contents($num); } ? -- /Daniel P. Brown daniel.br...@parasane.net || danbr...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Fri, Oct 2, 2009 at 2:37 PM, Ben Dunlap bdun...@agentintellect.com 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
-Original Message- From: Ben Dunlap [mailto:bdun...@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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
On Oct 2, 2009, at 15:22, Daevid Vincent wrote: -Original Message- From: Ben Dunlap [mailto:bdun...@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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
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. 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: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
Daevid Vincent wrote: -Original Message- From: Ben Dunlap [mailto:bdun...@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