PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Print Subscribe to PHP Subscribe to Newsletters

Advanced PHP Variables and Functions
Pages: 1, 2, 3

Variable variables and function names

Note: Variable functions and variables are a potential security risk if used improperly. Before using variable function calls or variables, it is important to consider possible security holes that could arise from malicious user input.



Beyond dynamic parameters, PHP also supports the ability to call functions dynamically, as well as use variables dynamically. That is, we can write scripts that call functions and access variables without knowing the names of the functions or variables until the script starts executing. We'll start with how this can be accomplished with variables first, and then conclude with a similar syntax to be used for executing functions.

Variable variables

It is possible to access a particular variable in PHP without even knowing its name until after the script starts executing. This is done through a special variable syntax that is defined as the following: ${<var name>} where the variable name is either a constant (such as "foo") or another variable containing a string representation of the name. Sound confusing? Why don't we look at some examples to help make things more clear:

<?php
  $foo = "This is the variable foo<br />";
  $bar = "This is the variable bar<br />";
  $foobar = "This is the variable foobar<br />";
  $foo_name = "foo";
  $bar_name = "bar";
	
  echo $foo;
  echo $bar;
  echo ${$foo_name};
  echo ${$bar_name};
  echo ${$foo_name.$bar_name};
?>

Above is what appears to be a fairly easy-to-understand script that simply echos different variables to the browser. However, the last three echo statements are referring to very strange looking variables. Consider the first of the last three echo statements. On this line, we are telling echo to print the variable whose name is contained within $foo_name. Because $foo_name's value is "foo", the echo statement will print the value of our PHP variable $foo to the browser. Although the second of the three is basically identical, the last statement is a little different. Here, we use the string concatenation operator '.' to combine the values of $foo_name and $bar_name to create the string "foobar" which we then use as the variable name for our last echo statement. The result of all of this is the following output:

This is the variable foo
This is the variable bar
This is the variable foo
This is the variable bar
This is the variable foobar

With this syntax, it is possible to write extremely complex scripts that are quite dynamic in nature with much less work then normally needed. Now that we have an idea on how to use variable variables, let's look at how to call functions based on the value of a variable.

Variable functions

Variable functions work on the same principal as variable variables. Basically given a variable containing a string representing a function in PHP, you can call the function simply by appending parentheses and any parameters to the end of the variable. For instance:

<?php
  function myfunc($msg) {
    echo $msg."<br />";
  }

  $func_name = "myfunc";
  $func_name("Hello, this is a test");
?>

will call the function myfunc() and pass our message "Hello, this is a test" to it. We accomplish this task by calling the function through the use of the variable $func_name that contains the actual name of the function myfunc.

Pages: 1, 2, 3

Next Pagearrow




Recommended for You

Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

programming

Articles that share the tag programming:

Rolling with Ruby on Rails (1374 tags)

Very Dynamic Web Interfaces (279 tags)

Ajax on Rails (231 tags)

Understanding MVC in PHP (202 tags)

A Simpler Ajax Path (186 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • Partner Sites
  • makezine.com
  • makerfaire.com
  • craftzine.com
  • igniteshow.com
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com