|
Posted by Oli Filth on 05/28/06 16:49
Rik said the following on 28/05/2006 12:22:
> Chung Leong wrote:
>> In PHP, because . and + have the same precedence, addition is no
>> longer communtative: "0." . 7 + 5 yields a different result from
>> "0." . 5 +
>> 7. So if you will, please explain how violating elementary
>> mathematical principles makes sense.
>
> Order matters, even in math.
> 5 - 7 != 7 - 5
>
> Actually, '.' & '+' are kin of the same operator, but for different types.
> It adds the piece on the right to the piece on the left. When '.', it
> handles it like a string, when '+', it handles it like a number.
Not sure about that. IMO, it's rather abstract to describe addition and
concatenation as related; they're not really doing the same thing at
all. Not least because concatenation is clearly not commutative.
By this logic, you could say that bitwise-OR is related, and therefore
deserved equal precedence.
> We're back
> to type juggling here, and using "0." as a string here is just making things
> blurry.
I agree with this.
A better example is:
5 * 6 . "Blah" => "30Blah"
"Blah" . 5 * 6 => "Blah30"
but:
5 + 6 . "Blah" => "11Blah"
"Blah" . 5 + 6 => 6
This discrepancy is (more or less) the issue in question.
> Thinking about it some more I think it boils down to this:
> People know 'simple' symobls like * / + - form an earl age. Later on, they
> will learn some more colpex opertors like log/ln/raising to a power.
>
> For some bizar reason, they are not willing to accept a 'complex' operation
> like 'take the literal symbolw of the right-part, and add those symbols to
> the right of the left part' to be a dot (.), and they expect some kind of
> precedence, while between / and * there is none, and + and - there is none,
> and people just read from left to right.
Yes, but {+,-} are "related" operations, so you would perhaps expect
them to have similar precedence. Equally, {*,/} are "related", but are
"unrelated" to {+,-}. If {+,-,*,/} all had the same precedence, things
would get messy and unintuitive. Similarly, as {.} is unrelated to any
of them, you would expect it to have a different precedence, but instead
it's given the same precedence as {+,-}.
Personally, I don't find it a problem in practice, as I
instinctively/automatically parenthesise anything where the precedence
is hazy or unclear. However, I'd still be interested to know why the
PHP developers thought that this choice of precedence made sense.
--
Oli
Navigation:
[Reply to this message]
|