Reply to Re: echo oddities...

Your name:

Reply:


Posted by Justin Koivisto on 01/09/06 19:43

rlee0001 wrote:
> Script in question:
>
> <?php
> echo '
> <!-- Search Summary -->
> <tr>
> <td colspan=\'8\' class=\'resultsfooter\'>
> <p>
> Showing results '.($offset + 1).' to '.$offset + $perpage.'
> </p>
> </td>
> </tr>';
> ?>
>
> When I run this simple command I get this output:
>
> 8
> </p>
> </td>
> </tr>
>
> The whole beginning of the string fails to print. I have also tried
> using PRINT instead of ECHO and get the same output. I have isolated
> this code from a much larger script and tested it on its own and I
> still get this partial output (with a 0 instead of the expected 8). Am
> I loosing my mind here or can anyone else out there duplicate this
> behaviour?

I can duplicate it. Even from within ZendStudio (5.1), and it is
expected... (see below)

> I just fixed it by putting the $offset + $perpage in ( and )'s. Is this
> an order of operations issue with the . (dot) operator? If so I must be
> an idiot.

It is an interesting little thing... Here's what is happening - it's all
about type conversion & expressions. Because . & + have the same
operator precedence, you need to look at the expressions in order from
the left:

<?php echo '1 ' + 1 . ' 1'; ?>
* ('1 ' + 1) == string('1 1')

* string('1 1') + 1 == int(2)
string is converted to int value before operation because it is the
lowest common type of the operands.

* int(2) . ' 1' == string('2 1')




<?php echo 'one' + 1 . ' 1'; ?>
* ('one' + 1) == int(1)
string is converted to integer value (0) before operation

* 0 + 1 == int(1)

* int(1) . ' 1' == string('1 1')


<?php echo '1' + 'one' + 1; ?>
* ('1' + 'one') == int(1)
again, conversion to int because "+" is not a string operator...

* int(1) + 1 == int(2)



HTH

--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com

[Back to original 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

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