Re: [PHP-DEV] Interesting result
I was talking about telling the 'undefined behaviour' case; not what happens with the values exactly. I don't see a drawback in documenting an 'undefined behaviour'. It's good for people to have a torough documentation which even warns them before they're doing silly things. Note that the majority of users of PHP are non-programmers. Warning them before they shoot themselves a good thing [tm]. On Mon, Dec 23, 2002 at 08:53:41AM -0800, David Gillies wrote : --- Markus Fischer [EMAIL PROTECTED] wrote: So shouldn't we document those special case on the operators page ? No. Any developer worth his salt knows not to do silly things like this. It's not a 'special case'. It's just undefined behaviour. If your code uses syntax like this it is broken. As for 'why', in the implementation sense, this happens, I would imagine that the reference, by increasing the refcount of the variable, moves evaluation of the postincrement operator to a different sequence point. Just don't do it. Modifying the value of a variable twice between sequence points yields UNDEFINED BEHAVIOUR. That mean anything at all can happen, up to and including your hard drive being reformatted and rude emails being sent to your boss. You do NOT neeed to know the details. As KR points out: if you don't know *how* [assorted weird tricks] are done on various machines, that innocence may help to protect you. Best Wishes David Gillies San Jose Costa Rica On Sun, Dec 22, 2002 at 10:26:08AM +0200, Andrey Hristov wrote : oukei, stopping to ask anymore :)) Andrey - Original Message - From: Andi Gutmans [EMAIL PROTECTED] To: Andrey Hristov [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, December 21, 2002 7:20 PM Subject: Re: [PHP-DEV] Interesting result Again, as it is undefined in PHP the question Why in itself is wrong :) Andi At 04:43 PM 12/21/2002 +0200, Andrey Hristov wrote: - Original Message - From: Andi Gutmans [EMAIL PROTECTED] To: Andrey Hristov [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, December 21, 2002 4:17 PM Subject: Re: [PHP-DEV] Interesting result It doesn't matter because the behavior here is undefined just like in C. You can't use a variable and it's post/pre increment value in the same expression. Two years ago I've been told by a professort that a+++a+++a depends on the language and the compiler. Yesterday i found this page : http://www.blueshoes.org/en/developer/syntax_exam/ If a=1 - Java : 6 PHP : 6 gcc 2.95 : 3 I know that is on the edge and I will never use it in real script by I was curious why the reference change the result. Best wishes, Andrey At 03:26 PM 12/21/2002 +0200, Andrey Hristov wrote: Hi, i got an interesting case : ?php $a = 1; var_dump($a + $a++); $a = 1; $x = $a; var_dump($a + $a++); ? Result int(2) int(3) -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Interesting result
In php.dev Markus Fischer [EMAIL PROTECTED] wrote: I was talking about telling the 'undefined behaviour' case; not what happens with the values exactly. I don't see a drawback in documenting an 'undefined behaviour'. It's good for people to have a torough documentation which even warns them before they're doing silly things. Note that the majority of users of PHP are non-programmers. Warning them before they shoot themselves a good thing [tm]. I think documenting this undefined behaviour is a good thing. Maybe something like this on language.operators.assignment.php: Assigning to a variable more than once in the same expression leads to undefined behaviour - this means you _might_ get the result you want, but this can change with the PHP version or the machine your code is running on, or even with the phase of the moon. Just don't do it. Don't do something like this: $b = ($a += 1) + ($a += 2); Instead use: $b = $a += 1; $b += ($a += 2); Between assignments to the same variable, always use a semicolon. If you're not sure about operator precedence, use parentheses. And almost the same on language.operators.increment.php: Incrementing or decrementing a variable more than once in the same expression leads to undefined behaviour - this means you _might_ get the result you want, but this can change with the PHP version or the machine your code is running on, or even with the phase of the moon. Just don't do it. Don't do something like this: $b = ($a++) + ($a++); Instead use: $b = $a++; $b += $a++; Between incrementing or decrementing the same variable, always use a semicolon. Regards... Michael -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Interesting result
oukei, stopping to ask anymore :)) Andrey - Original Message - From: Andi Gutmans [EMAIL PROTECTED] To: Andrey Hristov [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, December 21, 2002 7:20 PM Subject: Re: [PHP-DEV] Interesting result Again, as it is undefined in PHP the question Why in itself is wrong :) Andi At 04:43 PM 12/21/2002 +0200, Andrey Hristov wrote: - Original Message - From: Andi Gutmans [EMAIL PROTECTED] To: Andrey Hristov [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, December 21, 2002 4:17 PM Subject: Re: [PHP-DEV] Interesting result It doesn't matter because the behavior here is undefined just like in C. You can't use a variable and it's post/pre increment value in the same expression. Two years ago I've been told by a professort that a+++a+++a depends on the language and the compiler. Yesterday i found this page : http://www.blueshoes.org/en/developer/syntax_exam/ If a=1 - Java : 6 PHP : 6 gcc 2.95 : 3 I know that is on the edge and I will never use it in real script by I was curious why the reference change the result. Best wishes, Andrey At 03:26 PM 12/21/2002 +0200, Andrey Hristov wrote: Hi, i got an interesting case : ?php $a = 1; var_dump($a + $a++); $a = 1; $x = $a; var_dump($a + $a++); ? Result int(2) int(3) -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Interesting result
So shouldn't we document those special case on the operators page ? On Sun, Dec 22, 2002 at 10:26:08AM +0200, Andrey Hristov wrote : oukei, stopping to ask anymore :)) Andrey - Original Message - From: Andi Gutmans [EMAIL PROTECTED] To: Andrey Hristov [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, December 21, 2002 7:20 PM Subject: Re: [PHP-DEV] Interesting result Again, as it is undefined in PHP the question Why in itself is wrong :) Andi At 04:43 PM 12/21/2002 +0200, Andrey Hristov wrote: - Original Message - From: Andi Gutmans [EMAIL PROTECTED] To: Andrey Hristov [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, December 21, 2002 4:17 PM Subject: Re: [PHP-DEV] Interesting result It doesn't matter because the behavior here is undefined just like in C. You can't use a variable and it's post/pre increment value in the same expression. Two years ago I've been told by a professort that a+++a+++a depends on the language and the compiler. Yesterday i found this page : http://www.blueshoes.org/en/developer/syntax_exam/ If a=1 - Java : 6 PHP : 6 gcc 2.95 : 3 I know that is on the edge and I will never use it in real script by I was curious why the reference change the result. Best wishes, Andrey At 03:26 PM 12/21/2002 +0200, Andrey Hristov wrote: Hi, i got an interesting case : ?php $a = 1; var_dump($a + $a++); $a = 1; $x = $a; var_dump($a + $a++); ? Result int(2) int(3) -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DEV] Interesting result
Hi, i got an interesting case : ?php $a = 1; var_dump($a + $a++); $a = 1; $x = $a; var_dump($a + $a++); ? Result int(2) int(3) Can somebody explain why if there is reference to the var the result is different? Andrey -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Interesting result
It doesn't matter because the behavior here is undefined just like in C. You can't use a variable and it's post/pre increment value in the same expression. Andi At 03:26 PM 12/21/2002 +0200, Andrey Hristov wrote: Hi, i got an interesting case : ?php $a = 1; var_dump($a + $a++); $a = 1; $x = $a; var_dump($a + $a++); ? Result int(2) int(3) Can somebody explain why if there is reference to the var the result is different? Andrey -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Interesting result
- Original Message - From: Andi Gutmans [EMAIL PROTECTED] To: Andrey Hristov [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, December 21, 2002 4:17 PM Subject: Re: [PHP-DEV] Interesting result It doesn't matter because the behavior here is undefined just like in C. You can't use a variable and it's post/pre increment value in the same expression. Two years ago I've been told by a professort that a+++a+++a depends on the language and the compiler. Yesterday i found this page : http://www.blueshoes.org/en/developer/syntax_exam/ If a=1 - Java : 6 PHP : 6 gcc 2.95 : 3 I know that is on the edge and I will never use it in real script by I was curious why the reference change the result. Best wishes, Andrey At 03:26 PM 12/21/2002 +0200, Andrey Hristov wrote: Hi, i got an interesting case : ?php $a = 1; var_dump($a + $a++); $a = 1; $x = $a; var_dump($a + $a++); ? Result int(2) int(3) -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Interesting result
Again, as it is undefined in PHP the question Why in itself is wrong :) Andi At 04:43 PM 12/21/2002 +0200, Andrey Hristov wrote: - Original Message - From: Andi Gutmans [EMAIL PROTECTED] To: Andrey Hristov [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, December 21, 2002 4:17 PM Subject: Re: [PHP-DEV] Interesting result It doesn't matter because the behavior here is undefined just like in C. You can't use a variable and it's post/pre increment value in the same expression. Two years ago I've been told by a professort that a+++a+++a depends on the language and the compiler. Yesterday i found this page : http://www.blueshoes.org/en/developer/syntax_exam/ If a=1 - Java : 6 PHP : 6 gcc 2.95 : 3 I know that is on the edge and I will never use it in real script by I was curious why the reference change the result. Best wishes, Andrey At 03:26 PM 12/21/2002 +0200, Andrey Hristov wrote: Hi, i got an interesting case : ?php $a = 1; var_dump($a + $a++); $a = 1; $x = $a; var_dump($a + $a++); ? Result int(2) int(3) -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php