You are here: Re: Butt ugly « PHP Programming Language « IT news, forums, messages
Re: Butt ugly

Posted by Rik on 05/28/06 17:58

Oli Filth 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.

I admit I should have read more precise. I have now looked up the word
commutative in the dictionary (damned english), and you can scratch most of
the above remark....

> 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 {+,-}.

"related" is rather undefined here. I could make a case either way why + and
* are related, and why they are not, depending on what relation you're
talking about.

If one oversimplifies things, one could say adding (concating) strings is a
lot like adding numbers. Intuitively, you'd think that "hello" + "world" =
"helloworld" (which in some languages, it is). Because of PHP's ability to
cast variables on the fly there had to be another operator, so the operation
either used numerical value or strings. That might be the reason for the
same precedence. I'm not saying they are the same.

> Personally, I don't find it a problem in practice, as I
> instinctively/automatically parenthesise anything where the precedence
> is hazy or unclear.

It's just a simple fact of knowing/learning that the . has the same
precedence. Once, when we were little, we learned + & - had the same in
math, en / & * also the same. How hard is it to learn that . has the same
precedence as + & -?

And like I said earlier: when either getting confused, or wanting to
increase readability, brackets are the way to go. They don't HAVE to be
necessary to use them.

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация