Appending an integer to a string

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

    Appending an integer to a string

    I am having problems appending a number to the end of a string. I
    searched google and google forums and couldn't figure it out .... sorry
    i'm sure its simple.

    so I have:

    $filename = 'test.file.";
    $i = 0;

    and I want to change $filename to become 'test.file.0'.

    $filename .= $i; //doesn't work
    $filename += $i; //doesn't work
    $filename .= (string) $i; //doesn't work, nor does += (string) $i

    So, how could I do this using these variables?

    (I plan to do this inside a loop, and loop until test.file.i does not
    exist, at which point I will create one and save the news post. I will
    do the same to display the news on my front page -- loop through
    starting at i=0 and display the contents of the files until test.file.i
    does not exist)

    Cheers,
    -Rob

  • Steve

    #2
    Re: Appending an integer to a string

    [color=blue]
    > I am having problems appending a number to the end of a string. I[/color]
    [color=blue]
    > $filename = 'test.file.";
    > $i = 0;[/color]
    [color=blue]
    > and I want to change $filename to become 'test.file.0'.[/color]

    $filename = "$filename$ i";

    or

    $filename .= "$i";

    or

    $filename .= strval( $i );

    or

    $filename = sprintf( "%s%d", $filename, $i );

    or

    google some more...

    ---
    Steve

    Comment

    • Malcolm Dew-Jones

      #3
      Re: Appending an integer to a string

      Robizzle (Rob.Meyer1@gma il.com) wrote:
      : I am having problems appending a number to the end of a string. I
      : searched google and google forums and couldn't figure it out .... sorry
      : i'm sure its simple.

      : so I have:

      : $filename = 'test.file.";
      ^

      Mismatched quotes. It works for me when I use the correct quotes.

      $ php

      <?php

      $filename = 'test.file.'; # note quote
      $i=0;

      $filename .= $i;

      echo $filename;

      ?>
      ^D

      test.file.0




      --

      This programmer available for rent.

      Comment

      Working...