Evaluating Strings...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • number1.email@gmail.com

    Evaluating Strings...

    In php, I have an array called "q". The elements are:

    $q[0] = 2
    $q[1] = 4
    $q[2] = 5
    $q[3] = 3

    I'd like to be able to loop through this array via a While loop, and
    echo the contents of these array elements back to the screen.

    When I try something like:

    for($i = 0; $i < 4; $i++) {
    echo $q[i];
    echo eval("$q[i]");
    echo eval("\$q[" . i . "]");
    }

    I get the "String" instead of the "Contents". For example, the output
    is $q[0] instead of 2, $q[1] instead of 4, etc.

    How can I fix this? Thanks.

  • Oli Filth

    #2
    Re: Evaluating Strings...

    number1.email@g mail.com said the following on 11/12/2005 10:59:[color=blue]
    > In php, I have an array called "q". The elements are:
    >
    > $q[0] = 2
    > $q[1] = 4
    > $q[2] = 5
    > $q[3] = 3
    >
    > I'd like to be able to loop through this array via a While loop, and
    > echo the contents of these array elements back to the screen.
    >
    > When I try something like:
    >
    > for($i = 0; $i < 4; $i++) {
    > echo $q[i];
    > echo eval("$q[i]");
    > echo eval("\$q[" . i . "]");
    > }
    >
    > I get the "String" instead of the "Contents". For example, the output
    > is $q[0] instead of 2, $q[1] instead of 4, etc.
    >[/color]

    Use echo $q[$i].


    --
    Oli

    Comment

    • Alvaro G. Vicario

      #3
      Re: Evaluating Strings...

      *** number1.email@g mail.com escribió/wrote (11 Dec 2005 02:59:32 -0800):[color=blue]
      > for($i = 0; $i < 4; $i++) {
      > echo $q[i];[/color]

      Add this code on top of your script:

      error_reporting (E_ALL);

      It will warn you that you're using an undefined constant called "i".


      --
      -+ Álvaro G. Vicario - Burgos, Spain
      ++ http://bits.demogracia.com es mi sitio para programadores web
      +- http://www.demogracia.com es mi web de humor libre de cloro
      --

      Comment

      Working...