|
Posted by Rik on 05/27/06 21:09
Chung Leong wrote:
> Jerry Stuckle wrote:
>> Not if you understand precedence. Remember, '.' has a high
>> precedence. The following all work:
>
> The . operator has the same precedence as + and -, the wisdom of which
> is debatable. I don't think it's intuitive that
> echo 2+2 . ' = 2 + 2';
> prints out '4 = 2 + 2' while
> echo '2 + 2 = ' . 2+2;
> prints out 4.
The only thing WHY this isn't intuitive is because of the irregular use of
spaces. Why surround the . by spaces, and not the +? You're only making
thinks blurry for yourself.
Typing:
echo 2 + 2 . ' = 2 + 2';
echo '2 + 2 = ' . 2 + 2;
... is a lot clearer.
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.
Let's say $text = "2 + 2 = " or " = 2 + 2"
echo 2 + 2 . $text;
echo $text . 2 + 2;
It just makes sense to me....
Grtz,
--
Rik Wasmus
Navigation:
[Reply to this message]
|