Announcement

Collapse
No announcement yet.

help with variabales in variable names

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • help with variabales in variable names

    say i have:

    $answer0
    $answer1
    $answer2
    $answer3

    then i wana say:

    $i=0;
    while($i<4 && ___$answer.$i___ !=""){
    ......;}

    ___$answer.$i___ needs changing to sumthing that works, how could i do this?

  • #2
    I am not 100% sure, but I think you can use the eval() function. It evaluates a string as PHP code and then interprets it.

    http://www.php.net/

    -Vic

    Comment


    • #3
      i think u miss understood

      using $i=0;
      im trying to tell it to get the varible $answer0;

      Comment


      • #4
        Something like:

        PHP Code:
        $var = eval("\$answer$i"); 
        Should work. Untested, and I am not 100% sure, but you should use the eval() function.

        Search for it on php.net

        -Vic

        Comment


        • #5
          nope, didnt work

          Comment


          • #6
            PHP Code:
            while($i AND ${answer.$i} != '') { 
            ;

            or:
            PHP Code:
            while ($i 4) {
              
            $temp "answer$i";
              if ($
            $temp != '') {
                ;
              }

            or probably a better strategy:
            PHP Code:
            $answer = array();
            $answer[] = 'something';
            $answer[] = 'something else';
            $answer[] = ' and yet';
            $answer[] = 'something else again';
            foreach (
            $answer as $index => $value) {
              echo 
            "index -- $value<br />";

            or:
            PHP Code:
            $answer = array('something''something else'' and yet''something else again');
            foreach (
            $answer as $index => $value) {
              echo 
            "index -- $value<br />";

            Comment


            • #7
              thanx i used the top one and it worked perfectly.

              the reason i cant use an array is becoz the results are coming from a mysql db, and different amounts of rows will be present, i simplified it to make it easy to understand.

              seems to have worked

              thanx a lot!

              Comment


              • #8
                What freddie said.

                Well, you could use an array for what you described. Just make it multidimensional.

                -Vic

                Comment

                widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                Working...
                X