SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot IznSe7en's Avatar
    Join Date
    Jun 2004
    Location
    Around
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    !!!!Variable Swap!!!!(not what is sounds like)

    Question, got a script here, you dont really need to know much about it because im focusing on something independent here. BTW thanks in advance to whoever replies.

    Ok: ive got a script that posts to itself and sends an ID number. then

    $variable = '$array[' . $fileid . ']';
    so
    $variable = '$array[1]';

    Can i swap that so that the one on the right is the variable, i cant figure out how to get the name on the right to become a variable itself? Help?


    -Thanks,
    Izzy

  2. #2
    SitePoint Enthusiast mrsmiley's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use the $$ in your code. An example would be something like this.

    $foo = "bar";
    $bar = "testing";

    echo $$foo would output the text "testing" to the screen. The $$ means to use the text in that variable as the name of another variable. I think the manual called it variable variables.

    In your case (if I've interpreted it correctly), try this ...

    <?php
    $test[1] = 'cheese';
    $var = '$test[1]';
    $$var = 'testing';
    echo $$var;
    ?>

    It will output the text 'testing' rather than 'cheese'.

    Is this what you are looking for?

  3. #3
    SitePoint Zealot IznSe7en's Avatar
    Join Date
    Jun 2004
    Location
    Around
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    $test[1] = 'cheese';
    $var = '$test[1]';
    $$var = 'testing';
    echo $$var;
    ?>

    Thanks for the help, yet it doesnt really help. Look at the above code.
    The second and third line are irrelevant. $$var will echo cheese because it is defined as a variable, after the first $ im assuming php just sees at as another variable. Thanks for your help though, i am going to look up variable variables, sounds familier.

  4. #4
    SitePoint Zealot IznSe7en's Avatar
    Join Date
    Jun 2004
    Location
    Around
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All i need to do is have a certain number that is submitted from the previous form via post. And have it pertain to an area in the array.

    For example:

    $somevar = $_POST['somevar'];

    echo $somevar; //returns 4

    echo $myarray[4];

    Just gotta figure that one out, sigh....

  5. #5
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Hawaii
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems like you're looking for

    echo $myarray[ $somevar ];

    Assuming $somevar is 4, this will be equivalent to $myarray[ 4 ]. Is this what you were looking for?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •