# Butt ugly

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Coming from an Assembler/C/C++/C# background I got to say this is butt ugly:

<?php
echo "2 + 2 = " . 2+2; // This will print 4
echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
echo "test " . 2+2; // This will print 2
?>

R.

## Re: Butt ugly

Rik G. wrote:

Not if you understand precedence.  Remember, '.' has a high precedence.  The
following all work:

<?php
echo "2 + 2 = " . (2+2); // This will print 2 + 2 = 4
echo "2 + 2 = " , (2+2); // This will print 2 + 2 = 4
echo "test " . (2+2); // This will print test 4
?>

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Butt ugly

ugly:

The

OK, thanks, that looks better but still: why does it eat the strings "2 + 2
= " and "test " in case 1 and 3?

And here's for something even but uglier:

echo "2 + 2 = " . 2+3; // This will print 5
echo "2 + 2 = " , 2+3; // This will print 2 + 2 = 5
echo "test " . 2+3; // This will print 3

## Re: Butt ugly

Rik G. wrote:

See my other post.

1. String "2 + 2 = ".
2. String "2 + 2 = 2".
3. Gets cast to an integer because of +, which results in the first number:
integer "2".
4. Integer 2 + Integer 3 = 5

1. String "2 + 2 = " get's echoed seperately.
2. Calculation 2+3 is performed.
3. Integer 5 is echoed seperately (the , operator is way, way down the
precedence list).

1. String "test ".
2. String "test 2".
3. String gets cast to an integer, the first character is not a number or
'-' followed by a number, so gets cast to integer 0.
4. 0+3 = 3

Grtz,
--
Rik Wasmus

## Re: Butt ugly

Jerry Stuckle wrote:

Not only precedence, type juggling also plays a major part here.
Detailed:

1. The string "2 + 2 = " becomes "2 + 2 = 2" (indeed precedence).
2. Trying to add a number to this string casts it to 2 (the first number),
and adds 2, so gives 4.

The string "2 + 2 = " gets echoed seperately from the integer resulting from

1. The string "test " becomes "test 2".
2. Trying to add the number 2 to "test 2" casts the string to an integer
(0), and adding 2 gives indeed 2

Variable variables, without strict type, are a blessing in some cases, in
others it's immensely irritating. You just have to keep an eye on it :-).

http://nl3.php.net/manual/en/language.types.type-juggling.php
http://nl3.php.net/manual/en/language.operators.php
Table 15-1

When in doubt, cast the piece of code to a certain type by (type) (e.g.
(bool), (string), (int) etc.).

Grtz
--
Rik Wasmus

## Re: Butt ugly

[Snip]

from

OK, it's clear now (but still butt ugly).
Thanks for the explanation.

R.

## Re: Butt ugly

Rik G. wrote:

[snip]

It was your choice to write it like that. If you don't want the
freedom to write something so badly, then don't use a language that
lets you do it.

It's like that doctor/patient-joke:

[Patient]: doctor, it hurts when I do this.
[Doctor] : then don't do that.

/Bent

[snip]

## Re: Butt ugly

Jerry Stuckle wrote:

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.

## Re: Butt ugly

Chung Leong wrote:

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

## Re: Butt ugly

Rik wrote:

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

Well, it didn't make sense to the other Rik ;-)

## Re: Butt ugly

What
that

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

## Re: Butt ugly

Norman Peelman said the following on 28/05/2006 03:45:

Actually, that's doing:
'A + B = ' . 'A' + 'B'
'A + B = A' + 'B'
0 + 0
0

(the . is evaluated first, due to left associativity.)

Similarly for this one.

It's *all* about precedence (and associativity, indirectly), and that's
the point Chung was making.

The fact that the examples all contain things like "A + B = " is
irrelevant, and just confusing things...

--
Oli

## Re: Butt ugly

'string'

You are correct, and that was my intention to show that basically the 'A' +
'B' (right side of = sign) evaluated to 0 at some point; 'B' = 0...

strings

True!

The big problem I think is that Chung is comparing apples to oranges. The
'+' function causes non-number strings to be evaluated as zero (0) and the
'.' function causes type conversion.  The solution is to pay attention to
what the desired result is and use the '( )' parentheses to force the proper
precedence. See my post to Chung...

Norm

## Re: Butt ugly

'string'
strings

...second reply, not to mention that the original posters example:

<?php
echo "2 + 2 = " . 2+2; // This will print 4
echo "2 + 2 = " , 2+2; // This will print 2 + 2 = 4
echo "test " . 2+2; // This will print 2
?>

provides correct results as:

#1 "2 + 2 = " . 2 + 2
=     "2 + 2 = 2" + 2
=     2 + 2  <- type conversion causes the string to be evaluated as a
number thereby truncating '+ 2 = 2' leaving the first 2 in the string
=     4

#2 "2 + 2" , 2 + 2
is actaully two separate arguments to the echo function (use of ','), there
is no math or string concatination involed other than the appearance of the
final output

#3 "test" . 2 + 2
=   "test2" + 2
=   0 + 2 <- "test2" is assumed to not be a number as the string does not
begin with a number
=   2

Norm

## Re: Butt ugly

Norman Peelman wrote:

You might as well argue that 1 + 2 * 3 should equal 9.  "As soon as you
add 2 to 1, you get 3..."

Addition and multiplication are communtative operations. 1 + 6 = 6 + 1
and 2 * 3 = 3 * 2. Thanks to precedence, they maintains their
properties in more complex expression: 1 + 2 * 3 = 1 + 3 * 2 = 3 * 2 +
1.

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.

## Re: Butt ugly

Chung Leong wrote:

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. We're back
to type juggling here, and using "0." as a string here is just making things
blurry.

Your reasoning has another flaw, for instance: in math '-' & '+' have the
same precendence, and you're actually saying:
2 + 2 - 3 (1)
should equal
3 - 2 + 2 (3)
or
2 - 3 + 2 (1)
depending on wether you decide the - or the + has precedence.

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.

Don't you think it's strange that when handling just strings, there is
little to no confusion, but as soon as number play a part all hell breaks
loose? It's just a lack of abstract thinking IMO.

Grtz,
--
Rik Wasmus

## Re: Butt ugly

Hm,

my apologies for the terrible spelling throughout that post.
Once I get excited my english tends to suffer :-)

Grtz,
--
Rik Wasmus

## Re: Butt ugly

back
things

But allowed in PHP, a loosely typed language giving great flexibility to the
programmer

...there are two levels to the precedence rules and basically stated: when
to operators have equal precedence then left to right rules apply unless
otherwise noted.
see:
http://us3.php.net/manual/en/language.operators.php#language.operators.precedence

operation
none,

## Re: Butt ugly

Norman Peelman wrote:

I'm not saying it cannot be done, I'm saying that when you're talking about
abstract standards, using this example only makes it harder to see the
logic. The fact that it can be done in PHP saves the programmer a terrible
amount of coding. A blessing in most cases, sometimes an unwanted effect.

http://us3.php.net/manual/en/language.operators.php#language.operators.precedence

I know, like I stated earlier:

Grtz,
--
Rik Wasmus

## Re: Butt ugly

Rik said the following on 28/05/2006 12:22:

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.

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.

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