TheDesignspace
Construct a variable name in PHP from a string and another variable


February 24, 2008
Most Popular | PHP | Web Building

The "Shop" page of this site is a php page which displays an amazon a-store category and the second level tabs appropriate to that category, using a "node id". The html for the tabs is defined in an included page like this:
<?php


//-------------//
$tabs6= '<ul class="yui-navset bd">';
$tabs6.='<li class="first selected" id="tab6"><a href="shop.php?tab=6" >Web Development</a></li>';
$tabs6.='<li class="" id="tab180"><a href="shop.php?tab=180" >CSS</a></li>';
$tabs6.='<li class=" " id="tab183"><a href="shop.php?tab=183">Javascript</a></li>';
$tabs6.='<li class="last" id="tab181"><a href="shop.php?tab=181">DHTML</a></li>';
$tabs6.='</ul>';

$tabs180= '<ul class="yui-navset bd">';
$tabs180.='<li class="first " id="tab6"><a href="shop.php?tab=6" >Web Development</a></li>';
$tabs180.='<li class="selected" id="tab180"><a href="shop.php?tab=180" >CSS</a></li>';
$tabs180.='<li class=" " id="tab183"><a href="shop.php?tab=183">Javascript</a></li>';
$tabs183.='<li class="last" id="tab181"><a href="shop.php?tab=181">DHTML</a></li>';
$tabs180.='</ul>';

$tabs183= '<ul class="yui-navset bd">';
$tabs183.='<li class="first " id="tab6"><a href="shop.php?tab=6" >Web Development</a></li>';
$tabs183.='<li class="" id="tab180"><a href="shop.php?tab=180" >CSS</a></li>';
$tabs183.='<li class=" selected" id="tab183"><a href="shop.php?tab=183">Javascript</a></li>';
$tabs183.='<li class="last" id="tab181"><a href="shop.php?tab=181">DHTML</a></li>';
$tabs183.='</ul>';

?>
Each tab link has a parameter containing the node id:
http://thedesignspace.net/shop.php?tab=183
The value of "tab" from the query string is used to populate a variable "$tab" which is used in several places on the page. One of those spots is the line which prints out the correct bank of tabs. In this case, a new variable name is constructed by concatenating "$tab" with a string. The code that displays the correct bank of tabs is simply:
<?php 
print ${'tabs'.$tab}; 
?>
See this page (and Carel Solomon's note) for more information on concatenating strings and variables or two variables to construct a variable name dynamically: Comment from Carel Solomon on php.net Variables page
Posted by ellen at February 24, 2008 10:19 PM | TrackBack

 Comments

That last bit was extremely helpful. Thanks!

Posted by: eemitch on November 25, 2008 6:29 PM

This article helped me alot!

Posted by: Paul on January 8, 2009 9:55 AM

I've been searching for an hour for how to create variable names on the fly in PHP. Thank you!

Posted by: Kimberly on January 20, 2009 12:40 AM

That's GREAT. It also help me. Thanks ellen, You give us a small but precious concept.

Posted by: Asad Kamran on April 19, 2009 1:09 AM

Great! It was very helpfull!

Posted by: WebDesignCompany on August 30, 2009 9:51 AM

Thanks a lot..really helpfull..:)

Posted by: dilantha on September 12, 2009 4:23 PM

Thanks alot. Really helpful

Posted by: jarell blaq on July 16, 2010 9:34 AM
 Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?