Professional Web Applications Themes

How to make a variable named after the value of another variable? - PHP Development

Hello, How to make a variable named after the value of another variable? I know I can use smth like: $a="name"; $$a="hey"; and that will produce variable called $name with value "hey". But when I try something like: $a="_REQUEST['"; $b="']"; $c=$a."thename".$b; $$c="thevalue"; echo $c; foreach ($_REQUEST AS $key=>$val) echo "$key: $val\n" Then I have only a printout: _REQUEST['thename'] How to make this _REQUEST['thename'] a variable name? Ragards, Talthen...

  1. #1

    Default How to make a variable named after the value of another variable?

    Hello,
    How to make a variable named after the value of another variable?

    I know I can use smth like:

    $a="name";

    $$a="hey";

    and that will produce variable called $name with value "hey". But when I try
    something like:


    $a="_REQUEST['";
    $b="']";
    $c=$a."thename".$b;
    $$c="thevalue";
    echo $c;
    foreach ($_REQUEST AS $key=>$val)
    echo "$key: $val\n"

    Then I have only a printout:

    _REQUEST['thename']


    How to make this _REQUEST['thename'] a variable name?

    Ragards,

    Talthen


    Guest

  2. #2

    Default Re: How to make a variable named after the value of another variable?

    <pl> kirjoitti
    viestissä:defeb1$980$news.tpi.pl... 


    You're shooting fish in a barrel there. You've just misunderstood something
    very wrong when it comes to working with arrays. Try describing just what
    exactly you are trying to do (because I can't make a head or tail out of
    that. I understund the lines, but I don't see what the point of it all is...
    For example after

    $a="_REQUEST['";
    $b="']";
    $c=$a."thename".$b;
    echo $c;

    You say:
    "Then I have only a printout:
    _REQUEST['thename']"

    As if it shouldn't be. That's exactly what I would expect to appear. Maybe
    you meant to echo $$c. And one thing more... You cannot refer to arrays via
    variable variables the way yo are trying to do it. Here's how it would work:

    <?php
    $my_cool_array = array('fred' => 'flintstone', 'barney' => 'rubble');
    $my_cool_pointer = "my_cool_array";
    // Now you can refer to it like this:
    echo ${$my_cool_pointer}['fred']; // Prints "flintstone"

    // But this, on the other hand, won't work:
    $my_invalid_pointer = "my_cool_array['fred']";
    echo $$my_invalid_pointer; // Doesn't print anything
    ?>

    --
    SETI Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.berkeley.edu/>
    Kimmo Laine <com>


    Kimmo Guest

  3. #3

    Default Re: How to make a variable named after the value of another variable?

    "Kimmo Laine" <com>: 

    Well... I wanted it to be printed (and it is) but also I wanted the echo in
    the loop (which wasn't printed).
    I wanted to make an item in $_REQUEST named after the value in another
    variable. Now, I know that it should be done this way:

    $var="thename"
    $_REQUEST[$var]="the value";

    However I don't understand why this works:
    $var="thename";
    $$var="the value";

    and when I do:
    $var="$_REQUEST['thename']";
    $$var="the value";
    it doesn't... (and that's the way I wanted to do it in the previous post)

    Regards,
    Talthen



    Guest

  4. #4

    Default Re: How to make a variable named after the value of another variable?

    <pl> kirjoitti
    viestissä:defj3m$oe3$news.tpi.pl... 
    >
    > Well... I wanted it to be printed (and it is) but also I wanted the echo
    > in the loop (which wasn't printed).
    > I wanted to make an item in $_REQUEST named after the value in another
    > variable. Now, I know that it should be done this way:
    >
    > $var="thename"
    > $_REQUEST[$var]="the value";
    >
    > However I don't understand why this works:
    > $var="thename";
    > $$var="the value";
    >
    > and when I do:
    > $var="$_REQUEST['thename']";
    > $$var="the value";
    > it doesn't... (and that's the way I wanted to do it in the previous post)
    >[/ref]


    Okay, I'm making a wild guess, but try without the quotes
    $var=$_REQUEST['thename'];
    $$var="the value";

    --
    SETI Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.berkeley.edu/>
    Kimmo Laine <com>


    Kimmo Guest

  5. #5

    Default Re: How to make a variable named after the value of another variable?

    "Kimmo Laine" <com>

    Not working: [/ref]

    Not working: 

    Not working:
    $var="_REQUEST['thename']";
    $$var="the value";

    Maybe the only way is $_REQUEST[$var]; ?

    Regards,
    Talthen


    Guest

  6. #6

    Default Re: How to make a variable named after the value of another variable?

    Use single quotes, not double quotes.

    Also note:

    http://us2.php.net/variables.predefined

    Variable variables: Superglobals cannot be used as variable
    variables
    inside functions or class methods.
    "

    I can't find my book right now to be sure I remember this perfectly, but
    going back to the original post, I do something like this sometimes:

    $state = "texas";
    ${"var_state_".$state} = ".0825"; # Tax rate

    gives $var_state_texas = .0825

    But don't use double quotes if you want the literal name rather than the
    value -- use single quotes. Or leave the $ out of the double quotes....you
    don't need it.

    hth
    John

    <pl> wrote in message
    news:defmvv$6ut$news.tpi.pl... [/ref]
    >
    > Not working: 
    >
    > Not working:
    > $var="_REQUEST['thename']";
    > $$var="the value";
    >
    > Maybe the only way is $_REQUEST[$var]; ?
    >
    > Regards,
    > Talthen
    >
    >[/ref]


    Tex Guest

  7. #7

    Default Re: How to make a variable named after the value of another variable?

    <pl> wrote in message
    news:defmvv$6ut$news.tpi.pl... [/ref]
    >
    > Not working: 
    >
    > Not working:
    > $var="_REQUEST['thename']";
    > $$var="the value";
    >
    > Maybe the only way is $_REQUEST[$var]; ?
    >
    > Regards,
    > Talthen
    >[/ref]

    Assume $_REQUEST['thename'] == 'Talthen' ...

    $var = $_REQUEST['thename']; //will set $var == 'Talthen'
    $$var = 'value'; //will create the variable $Talthen and set it to 'value'
    echo $Talthen; // will output: value

    ....so...

    foreach ($_REQUEST as $key => $val)
    {
    $$key = $val;
    echo "Variable created: $key == $val \n\r";
    }

    would create variables from your FORM elements (everything in REQUEST
    really)...

    Please give an example of the imput expected and output expected.

    Norm
    ---
    FREE Avatar hosting at www.easyavatar.com



    Norman Guest

  8. #8

    Default Re: How to make a variable named after the value of another variable?

    "Norman Peelman" <rr.com>
    This is how I solved thr problem:
    $var="thename"
    $_REQUEST[$var]="the value";
    It creates the variable $_REQUEST['thename']="the value". I just wondered
    how to do it with $$ method. However it seems that global variables cannot
    be variable variables in another way, that $_REQUEST[$var]="the value";

    Regards,
    Talthen


    Guest

  9. #9

    Default Re: How to make a variable named after the value of another variable?

    <pl> wrote in message
    news:dehd34$itb$news.tpi.pl... 


    Why do you write to $_REQUEST in the first place? $_REQUEST is the array
    that holds all the userinputs, so why would you want to write into that? And
    also why would you use variable variables method for this, when a simple
    array reference is much more simple. This makes no sense at all to me.

    --
    Welcome to Usenet! Please leave tolerance, understanding
    and intelligence at the door. They aren't welcome here.
    eternal piste erection miuku gmail piste com


    Kimmo Guest

  10. #10

    Default Re: How to make a variable named after the value of another variable?

    "Kimmo Laine" <com> 

    Well... it's an ultra hacking method :P
    I have an engine php script and some templates in php script. It is much
    easier for me to change the engine, than all the templates.
    All of this is working that way:
    Engine1 from template1 is generating a website, I click OK and then the date
    is send to engine2, which is generating the webpage using template2.
    It worked fine, but I had to make new fields available in all templates used
    by engine2. So it was much easier to add new $_REQUEST items in the engine2
    (rest of $_REQUEST is passed from engine1) :]
    It's a bit tricky, but works fine now :)

    Regards,
    Talthen



    Guest

Similar Threads

  1. #39251 [NEW]: variable variable class array property is read only
    By taskfreak at gmail dot com in forum PHP Bugs
    Replies: 1
    Last Post: October 25th, 07:08 AM
  2. variable named field calculated from variable named fields
    By Chris Kemp in forum Macromedia ColdFusion
    Replies: 1
    Last Post: June 24th, 07:36 PM
  3. Make a set variable a link
    By chebultz in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: May 11th, 07:27 AM
  4. #22237 [Com]: PHP crashes when class references property using variable variable
    By rep at devdomain dot com in forum PHP Development
    Replies: 0
    Last Post: September 29th, 11:34 AM
  5. Get html page title and make a temporary variable
    By Rich in forum PERL Beginners
    Replies: 1
    Last Post: August 13th, 01:50 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139