SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed with eval and value of variable name

    hello,

    I am trying to create a variable from 2 strings - one of which gets it's value from the name of another variable. Then i would like to execute the value of the varibale as a varaible, NOT as a string. I am guessing that this will involve eval() as part of the code, but I'm not sure how to implement all of this.


    example info here:
    Code:
    $file = "filename.jpg";
    $file_size = 2222;
    i want to:

    a) create a string with the value of the variable name '$file' , so:

    Code:
    $str_filesize = "$file"; // (and NOT) "filename.jpg" 
    // *Any idea on how to do this?


    b) then append "_size" to $str_filesize;

    Code:
    $str_filesize = $str_filesize . "_size";
    So now the value of $str_filesize would be the string '$file_size' and NOT 2222.


    c) convert the value $str_file into the value of the variable $file_size

    Code:
    *helpful code here*
    Now the value of $str_filesize should be the same value as the var $file_size, so $str_filesize should now be equal to 2222.


    Any help greatly appreciated!!!

  2. #2
    SitePoint Evangelist galt's Avatar
    Join Date
    Apr 2002
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure I fully understand your example. And I have never played with this stuff in PHP, so I can't be of much help. But maybe all you need is a nudge?

    Take a look at $$notation. By using TWO $$ in front of variable name instead of one, you reference the base property of the variable, not its contents. Reference by address vs reference by value. I hope this helps, and that it is a push in the RIGHT direction. Let me know...

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Galt, can you show me an example using $$ ?

  4. #4
    SitePoint Evangelist galt's Avatar
    Join Date
    Apr 2002
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope. I told you all I know about it. I have used this type of approach in other languages, and I remember seeing it ($$) when I was reading a PHP manual, but I would be stumbling my way thru a demo. I don't generally like code that changes variable names, IMHO it is hard to maintain later.

    I don't know if this is the right approach for your problem or not, but I suspect it is. I took a few minutes to find it in the PHP manual for you.

    http://www.php.net/manual/en/languag...s.variable.php

    Good Luck.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Galt,

    Thanks for the feedback (I had a look at the link that you supplied), but I don't think that this is what I need. Unless a variable variable can somehow give me the name of a variable (and NOT the variable value) as a sring value.


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
  •