|
Posted by Norman Peelman on 05/28/06 05:45
"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:1148755585.431248.282160@j55g2000cwa.googlegroups.com...
> Rik wrote:
> > Further, it's like any Western language: we read from left to right. If
> > precedence doesn't sort it out, it's processed as you write. Type juggle
> > necessary for the last operations stands. It seems very logical to me.
What
> > can make it confusing, is the fact that the string contains characters
that
> > could be operators. That could throw you of, just because you expect
> > something that you know since you were 6.
>
> That's sort of my point. Precedence should determine the outcome
> instead the order of the operators. Giving operators that are
> fundamentally different the same precedence is confusing, because
> people tend to group operations they consider similiar together in
> their mind. Can you imagine how confusing it would be if + and == has
> the same precdence? Quickly, what's the result of the following:
>
> 2 + 2 == 1 + 3
>
> > Let's say $text = "2 + 2 = " or " = 2 + 2"
> >
> > echo 2 + 2 . $text;
> > echo $text . 2 + 2;
> >
> > It just makes sense to me....
>
> Well, it didn't make sense to the other Rik ;-)
>
It makes sense because as soon as you try to ADD a number to a 'string'
(or a 'string' to a 'string'), the strings are lost (counted as zero):
echo 'A + B = ' . 'A' + 'B';
will output zero (0)
'A + B = ' . 0 + 0 = 0
0 . 0 = 0
0 = 0
echo 'A + B = ' . 'A' + 'B' + 100;
will output 100 (0 + 100)
'A + B = ' . 0 + 0 + 100 = 100
0 . 0 + 100 = 100
0 + 100 = 100
100 = 100
echo 'A + B = ' . 100;
will output A + B = 100
as the number is converted to string for concatination NOT addition. It's
not really about precidence at all as no math is done within quoted strings
(literals).
....but, you can:
echo 'A + B = ' . ('100' + '200') --> A + B = 300
echo 'A + B = ' . ('100A' + '200B') --> A + B = 300
but not...
echo 'A + B = ' . ('A100' + '200B') --> A + B = 200
'A100' is converted to zero (0)
Norm
Navigation:
[Reply to this message]
|