An endless loop

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

    An endless loop

    This is a continuation of a previous thread:"Display ing text with
    include file." This is the PHP include file.

    I have about googled myself out on this one. If I have found what it is
    I seek , I don't recognize it.

    ==========#
    <?php
    $data = file("outing.1. php");
    foreach ($data as $line) {
    $line = explode("_", $line);
    print $line['0'];
    }

    ?>
    ============#
    This code does exactly as expected. It prints to the screen the first
    element of the array.

    The array is of unknown length. I need to print to the screen all
    elements. I have been able to do this with: for loops, while loops, do
    loops, and some if statements, the problem is stopping when it reached
    an empty element.

    Even if I chose an arbitrary number to stop at, it would print the
    elements and the balance in error lines.

    Anybody have suggestions on how to stop the endless loop?

    TIA
    Dave
  • Bob Stearns

    #2
    Re: An endless loop

    Dave Kelly wrote:
    This is a continuation of a previous thread:"Display ing text with
    include file." This is the PHP include file.
    >
    I have about googled myself out on this one. If I have found what it is
    I seek , I don't recognize it.
    >
    ==========#
    <?php
    $data = file("outing.1. php");
    foreach ($data as $line) {
    $line = explode("_", $line);
    print $line['0'];
    }
    >
    ?>
    ============#
    This code does exactly as expected. It prints to the screen the first
    element of the array.
    >
    The array is of unknown length. I need to print to the screen all
    elements. I have been able to do this with: for loops, while loops, do
    loops, and some if statements, the problem is stopping when it reached
    an empty element.
    >
    Even if I chose an arbitrary number to stop at, it would print the
    elements and the balance in error lines.
    >
    Anybody have suggestions on how to stop the endless loop?
    >
    TIA
    Dave
    The answer lies in your own code: foreach($line as $token) print $token;

    Comment

    • Erwin Moller

      #3
      Re: An endless loop

      Dave Kelly wrote:
      This is a continuation of a previous thread:"Display ing text with
      include file." This is the PHP include file.
      >
      I have about googled myself out on this one. If I have found what it is
      I seek , I don't recognize it.
      >
      ==========#
      <?php
      $data = file("outing.1. php");
      foreach ($data as $line) {
      $line = explode("_", $line);
      print $line['0'];
      That looks strange to me.
      You use an associative array notation, when you clearly need a normal index.
      $line is an array.
      $line[0] contains the first element.
      $line[1] contains the second element.
      etc

      I don't uderstand why you use '0' as index instead of a plain 0.
      I am surprised this works at all. ???
      Is PHP converting the string '0' to integer 0?

      Erwin Moller


      Comment

      Working...