PHP Variable Variables

In PHP sometimes you may want to have variable variables or dynamic function calls for whatever reason. PHP allows you to programmatically construct variable and function names. Here is an example of a variable function call from within a class. Thsi would call the function named myFunc:

$variable = 'myFunc';
$this->{$variable}();

Sometimes you may want to create variable names on the fly, let's say in a loop where you are updating many records from a form post. This code would make a variable named commonCust and assign it a value of 1:

$custom = 'Cust';
${'common'.$custom} = '1';

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank

Home | PHP | PHP Variable Variables