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

Posted by Norman Peelman on 05/28/06 15:54

"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:1148792271.215334.320870@j55g2000cwa.googlegroups.com...
> Norman Peelman wrote:
> > 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):
>
> You might as well argue that 1 + 2 * 3 should equal 9. "As soon as you
> add 2 to 1, you get 3..."
>

....but that's not how the precedence is defined, the '*' multiplication has
higher precedence than '+' addition.


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

Your 'mathematical' examples are correct. Your last example is not... you
are taliking about two different things. The '.' operator is NOT
mathematical addition. The last example provides the correct answers:

"0." . 7 + 5 =
"0.7" + 5 =
5.7

since in this example the '.' is encountered first it must act on what it
knows and it knows what is to the immediate left and immediate right and
that is a 'string' and a number. The number 7 is converted to a string and
CONCATED to "0." making "0.7", then the '+' is encountered and it applies
itself to "0.7" and 5 resulting in a number whos value is 5.7 because the
string contains no leading alpha characters is converted to a number
(float/double) for the ADDITION process resulting in .7 + 5 = 5.7

....if you want the same answer then you must tell PHP how to handle the
precedence, so in this case you want to separate the math from the string
concatination:

".0" . (7 + 5) = "0.12"

(7 + 5) will always be computed as a single unit.

The bottom line is this... precedence can only 'guess' so much as to what
you the programmer wants and it normally does a great job. But when you
start mixing two different functions ('.' and '+') it has to make
assumptions, and those assumptions are to work on what is immediately known
and that is what is to the immediate left and right:

$str = 'C' . 'A' . 'T';

is computed/concated by:

$str = 'C' . 'A' . 'T';
$str = 'CA' . 'T';
$str = 'CAT';

and...

$str = 'C' . 'A' . 'T' + 100;
$str = 'CA' . 'T' + 100;
$str = 'CAT' + 100;
$str = 0 + 100;
$str = 100;

.... the '+' ADDition function causes non-numeric strings to be zero (0)

and...

$str = 'C' . 'A' . 'T' . 100;
$str = 'CA' . 'T' . 100;
$str = 'CAT' . 100;
$str = 'CAT100';

.... the '.' CONCATination function causes type conversion

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

Norm

 

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

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