PHP variable  variables

PHP variable variables

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on April 7, 2009

Over the coming weeks I’m going to be writing some posts on object orientated programming in PHP5, but to get us started there are a few techniques that we need to know, so today I’m going to be looking at variable variables.

Naming conventions are really important when it comes to variable names in programming and one way we can get around this is by using the value of the variable string as the variable name. to explain this better i’ve put together the following code.

$a = 'Ashley';
$b = 'Papermashup';
$c = 'John';
$d = 'Mark';
$e = 'Luke';

$values = array('a', 'c', 'd');
foreach($values as $seat){
	echo $$seat .'<br />';
}

Lets start by looking at the end of the code and more specifically the echo statement. Notice how there are two $ signs, this is because we are saying take the value of $seat and use it as the variable name and we do that by using $$. The rest of the code is pretty self explanatory, a simple array which we loop through to return the values of a, c and d.

Variable variables are a good way of trimming down unnecessary code.

demo

More tutorials from Papermashup
Comments
4 discussions around PHP variable variables
  1. Ben says:

    Well I didn’t know that, thank you :p

  2. Oğuz ÇELİKDEMİR says:

    Thanks Laszlo, keep up the good working.

  3. Laszlo Korte says:

    @hassan

    inside the loop $seat will be first “a”, then “c” and at last $set will be “d”.
    but we do not want to write:
    a
    c
    d

    but we want to output the content of the variables $a $c and $d:

    Ashley
    John
    Mark

    so $$seat calls the variable that is named like the content of $seat, at first $a, then $c, then $d.

    sorry for my bad english

  4. Hassan says:

    I didn’t get it… Why did you use $$ instead of $var? Could you explain a little more?

    I gotta say, I’m new to OO PHP so I really appreciate your post series on this topic.

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.