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

Posted by Norman Peelman on 05/28/06 16:49

Second reply...

> Norman Peelman said the following on 28/05/2006 03:45:
> > "Chung Leong" <chernyshevsky@hotmail.com> wrote in message
> > news:1148755585.431248.282160@j55g2000cwa.googlegroups.com...
> >> Rik wrote:
> >>> Let's say $text = "2 + 2 = " or " = 2 + 2"
> >>>
> >>> echo 2 + 2 . $text;
> >>> echo $text . 2 + 2;
> >>>
> >>> It just makes sense to me....
> >> Well, it didn't make sense to the other Rik ;-)
> >>
> > 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
>
> Actually, that's doing:
> 'A + B = ' . 'A' + 'B'
> 'A + B = A' + 'B'
> 0 + 0
> 0
>
> (the . is evaluated first, due to left associativity.)
>
>
> > 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
>
> Similarly for this one.
>
>
> > It's
> > not really about precidence at all as no math is done within quoted
strings
> > (literals).
>
> 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...
>
>

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

 

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

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