#1
  1. No Profile Picture
    hicc01
    Guest
    Devshed Newbie (0 - 499 posts)
    Does anyone know how to combine two variables into one? I have two variables containing information.

    $a and $i
    $a = a1 is a variable sent from a previous page, and I want to output the variable within a loop because there are more variables (a1, a2, a3, a4 .....)

    so what I'm trying to do is this:

    $i = 1
    while ( $i <= 4 ) {

    here I want to echo a1 by $a&$i Does anyone know how to put these two together? so I can output in the loop?

    $i++;
    }

    thanks, I hope you guys can understand what I'm asking for....
  2. #2
  3. No Profile Picture
    ExPulse
    Guest
    Devshed Newbie (0 - 499 posts)
    $hello = 'Hello ';
    $world = 'World';

    $hw = "$hello"."$world";

    echo $hw;

    This combines the two strings to form one and would print 'Hello World'
  4. #3
  5. No Profile Picture
    horaR
    Guest
    Devshed Newbie (0 - 499 posts)
    If I understand well you have:
    $a1 = "Var 1";
    $a2 = "Var 2";
    $a3 = "Var 3";
    and you want to access them in a loop.
    Try
    $i = 1;
    while ($i < 4) {
    eval("$a = "$a$i";");
    print("a$i = $an");
    $i++;
    }

    Be carefull with the escaped (with the slash) symbols.
    I hope this works.
  6. #4
  7. No Profile Picture
    moderator
    Guest
    Devshed Newbie (0 - 499 posts)
    Alternatively you could do it like this:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $a1 = "Var 1";
    $a2 = "Var 2";
    $a3 = "Var 3";

    $i = 1;
    while ($i < 4) {
    $temp="a".$i;
    print "a$i = $$tempn";
    $i++;
    }
    [/quote]
    Just a matter of personal prefrence, I guess.
  8. #5
  9. No Profile Picture
    horaR
    Guest
    Devshed Newbie (0 - 499 posts)
    Maybe it's my fault but the above code (by moderator) does not work for me.
    I am using PHP 3.0.12, and the ouput of the script was:
    a1 = $a1
    a2 = $a2
    ...
    where it should be:
    a1 = Var1
    a2 = Var2
    ...
    The aditional '$' did not work for me and left me a doubt:
    Is that an option in PHP.INI or a compile time option?
    thanks.

Similar Threads

  1. simple, full wxPython examples?
    By theperfectsoup in forum Python Programming
    Replies: 2
    Last Post: February 5th, 2004, 05:25 PM
  2. simple form, simple trigger, whats wrong ??
    By zishto in forum Oracle Development
    Replies: 3
    Last Post: January 12th, 2004, 01:25 PM
  3. Replies: 1
    Last Post: January 7th, 2004, 01:39 AM
  4. need help: simple java game
    By slinky2000 in forum Java Help
    Replies: 0
    Last Post: December 1st, 2003, 03:57 PM
  5. simple image gallery
    By runnerboy800 in forum PHP Development
    Replies: 2
    Last Post: November 14th, 2003, 05:02 AM

IMN logo majestic logo threadwatch logo seochat tools logo