You are here: Re: newbie syntax question « PHP Programming Language « IT news, forums, messages
Re: newbie syntax question

Posted by rlee0001 on 02/12/06 23:11

leegold,

Two things really:

1) The period in the second line you post should have been a semi-colon
to seperate the two commands. Alternatively you could have omitted the
second "print".

2) Be careful with "order of operations" in PHP when using the dot (.)
operator. When you use the dot operator always to brackets around any
equations you use. For example instead of:

print '12==12 : '.12==12

....you might instead use:

print '12==12 : '.(12==12)

....and even more correctly you would convert (12==12) to a string
before concat-ing it. I'll let you figure out the best way to do that.

Now as to figure out why your program gives the odd '112 == 12 : 1':
The first thing PHP does here is evaluate 12 == 12, converts it to a
string so that it can be passed to the print function and then it
passes that result to the print function. The result is that a '1' is
sent to output. The print function always returns true. It converts
this return value and converts it to a string to be used with the dot
(.) operator. Next it takes the string '12 == 12 : ' and appends the
string representation of the return value of the second print function.
It passes the combined string to the print function which sends '12 ==
12 : 1' to output. But the 1 on the end is not the 12 == 12 equation
result but rather print's return value! The output to the client is
thus:

112 == 12 : 1

+ The first '1' is the result of (12 == 12).
+ The '12 == 12 : ' is the string you specified.
+ The second '1' is the return value from the second print command.

To demonstrate, try this:

print '12 == 11 : ' . print 12 == 11;

....(note the 11 instead of the 12). You get this output now:

012 = 11 : 1

-Robert

 

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

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