Truly bizzare 'echo' problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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:

  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:

  echo 'Records ' . $start+1;
  echo ' through ' . $start+$per_page;
  echo ' of ' . $rec_count . '<p>';

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.


Re: Truly bizzare 'echo' problem

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


Re: Truly bizzare 'echo' problem

Quoted text here. Click to load it

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,
  Providenza & Boekelheide, Inc.

Re: Truly bizzare 'echo' problem

Thanks for that explanation. It makes perfect sense now, where before
it was just a strange and inexplicable quirk of the language.


Site Timeline