Truly bizzare 'echo' problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fiziwig

    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=$_GE T['rc'];
    $start=$_GET['sp'];
    echo 'Records ' . $start+1;
    echo ' through ' . $start+$per_pag e;
    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_pag e;
    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

  • fiziwig

    #2
    Re: Truly bizzare 'echo' problem

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

    --gary

    Comment

    • Tim Roberts

      #3
      Re: Truly bizzare 'echo' problem

      "fiziwig" <fiziwig@yahoo. com> wrote:
      [color=blue]
      >Never mind. I figured it out. A php newbie brain fart. s/b echo
      >'Records' . ($start+1); with parens.[/color]

      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, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • fiziwig

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

        --gary

        Comment

        Working...