SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Append a variable to a variable to form a different variable

    How can you append a variable to a variable, to make it lookup a different name?

    ie:
    PHP Code:
    public function Test($var)
    {
      echo 
    $this->Item->Area_$var;
      die;

    So if I did:
    PHP Code:
    Test('51'); 
    it would really do this:
    PHP Code:
    $this->Item->area_51

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use arrays

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,687
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by JREAM View Post
    How can you append a variable to a variable, to make it lookup a different name?
    By using variable variables.

    Although, with a better idea of what you're wanting to achieve, a more appropriate technique may be available to you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Technically it's not a variable variable I'd say, but ho hum.
    PHP Code:
    $this->items->area_{$var}; 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you can post your real problem then there might be some better solutions rather than that. But variable variables (as Paul already suggested) is what you are tying to use. But still let us know why you are doing that. I am quite confused while you know the prefix of the variable 'Area_' then why not just call them directly as normal.
    PHP Code:
    Test('Area_51'); 
    But you can still append the variable like this:
    PHP Code:
    class myClass{
        private 
    $a_1;
        
        public function 
    __construct(){
            
    $this->a_1 'Test';
        }
        
        public function 
    getM($a){
            return 
    $this->$a;
        }
    }
    $counter 1;
    $obj = new myClass();
    $v 'a_' $counter;
    echo 
    $obj->getM($v); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Technically it's not a variable variable I'd say, but ho hum.
    PHP Code:
    $this->items->area_{$var}; 
    Umm... the way Anthony has posted above did not work for me before when I was also trying to do so. Anthony, can you please say why the following code does not work for me to return the text 'Test' though it does not give any error? What I am doing wrong in the following code?
    PHP Code:
    class myClass{
        private 
    $a_51;
        
        public function 
    __construct(){
            
    $this->a_51 'Test';
        }
        
        public function 
    getM($a){
            return 
    $this->a_{$a};
        }
    }
    $obj = new myClass();
    echo 
    $obj->getM('51'); 
    But the code that I have posted in my previous post does work and that is what I had done before.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I didn't test it.

    This works, but is probably wrong too!

    The requirement to concatenate to the string surprised me.

    PHP Code:
    <?php
    class HTMLSpecificationFinder
    {
        protected
            
    $html4 'Version 4',
            
    $html5 'Version 5';
            
        public function 
    getSpec($version){
            return 
    $this->{'html' $version};
        }
    }

    $obj = new HTMLSpecificationFinder();
    echo 
    $obj->getSpec(5); #Version 5
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its hard to explain

    there are a lot of different $pageTypes, and want to grab that groups settings ie:
    $this->Settings->notify_subj_{$PageType}
    $this->Settings->notify_msg_{$PageType}

    I could do a switch statement but I think that would remove the flexibility of it.

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh... I don't want to say that it is wrong but I had tried rest of the other ways except this one:
    PHP Code:
    $this->{'html' $version}; 
    Thank you Anthony!
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •