Function from string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bogha
Forum Newbie
Posts: 8
Joined: Tue Aug 05, 2008 2:06 am

Function from string

Post by bogha »

hi

is there any way that i can convert a php statement into a function call

example:

Code: Select all

 
"level".$_GET['level'].($tag,$position);
 
i want this to be treated as a call to function
level1($tag,$position)

i've tried to use eval but i didn't work
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: Function from string

Post by devendra-m »

eval — Evaluate a string as PHP code
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Function from string

Post by RobertGonzalez »

Code: Select all

<?php
function dynFunc($arg) {
  return $arg . ' was passed';
}
 
$var = 'dynFunc';
$str = 'This is a string';
echo $var($str);
 
// Result: This is a string was passed
?>
They are called dynamic functions. yes you can do this. Be careful when doing so.
Post Reply