On Wed, 12 Mar 2003, Andrey Hristov wrote:
> Few minutes ago I found the following behaviour somehow wierd for me : > <?php > $a = 1; > $b = $a==1? 4:$a==2? 5:6; > printf("a[%d]b[%d]\n", $a, $b); > ?> > Prints : > a[1]b[5] > > Similar C program : > main() > { > int a,b; > a = 1; > b = a==1? 4:a==2? 5:6; > printf("a[%d]b[%d]\n", a, b); > } > Prints : > a[1]b[4] > > -=-=-=-=-=- > I think that the behavior of the C program is the right
It's just a different operator precedence; it's not really wrong, just different.
Where is the different precednece here? I can only find an error. Lets support parantesis:
ALL BUT PHP) '==' has higher precedence than '?:'
((a==1) ? 4 : ((a==2) ? 5 : 6)) => (1) ? 4 : ((0) ? 5 : 6) => 1 ? 4 : 6 => 4
PHP?) '?:' has higher precedence than '=='
(a == (1 ? 4 : a) == (2 ? 5 : 6))
( a == (4) == (5))
Now what? Assume order left from to right: ( (a == (4)) == (5) => (0 == 5) => 0
Or right to left (which contradicts rest of PHP behavior): ( a == ((4) == (5))) => (a == 0) => 0
Result: This is (to say it in german "mumpitz") wrong.
So lets fix it.
marcus
-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php