470,849 Members | 1,042 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Truly bizzare 'echo' problem

This was origianlly one line and was acting strange so I divided it
into three lines and it still acts the same strange way:

Code, cut and pasted from script:

....
$rec_count=$_GET['rc'];
$start=$_GET['sp'];
echo 'Records ' . $start+1;
echo ' through ' . $start+$per_page;
echo ' of ' . $rec_count . '<p>';
....

emitted HTML cut and pasted from the browser:

135 of 49<p>

$start is 0, $per_page is 35 and $rec_count is 49.

This is at the outermost leve, not inside any conditionals, and in
desparation I even stripped out nearby comments. What am I missing
here? the word 'of' gets echoes fine but the other words don't get
echoed at all. I'm really stumped.

Finally, I cut and pasted those three lines along with setting the
variables into an completely empty file that looks like this:

<html>
<head>
<title>temp</title>
</head>
<body>
<?
$start=0;
$per_page=35;
$rec_count=49;
echo 'Records ' . $start+1;
echo ' through ' . $start+$per_page;
echo ' of ' . $rec_count . '<p>';
?>
</body>
</html>

and the emitted HTML is still: 135 of 49<p>

instead of: Record 1 through 35 of 40<p>

FWIW: I'm doing this on a Yahoo/Geocities whosted web site.

--gary

Mar 16 '06 #1
3 1159
Never mind. I figured it out. A php newbie brain fart. s/b echo
'Records' . ($start+1); with parens.

--gary

Mar 16 '06 #2
"fiziwig" <fi*****@yahoo.com> wrote:
Never mind. I figured it out. A php newbie brain fart. s/b echo
'Records' . ($start+1); with parens.


That's correct, but do you understand why? The "." and "+" operators have
equal precedence, so the expression gets evaluated left-to-right.

echo 'Records ' . $start+1;

It computes "'Records ' . $start", which it does by converting $start to a
string, producing "Records 0". It then tries to add 1 to it, which it does
by converting the string to an integer. The string becomes 0, so the
result is 1, and that's what gets echoed.

One of the hazards of working with a loosely-typed language.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 17 '06 #3
Thanks for that explanation. It makes perfect sense now, where before
it was just a strange and inexplicable quirk of the language.

--gary

Mar 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Richard Gutery | last post: by
8 posts views Thread by BCC | last post: by
1 post views Thread by Zri Man | last post: by
2 posts views Thread by the.duckman | last post: by
32 posts views Thread by Request-1 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.