Page 1 of 1

Function from string

Posted: Tue Aug 05, 2008 2:10 am
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

Re: Function from string

Posted: Tue Aug 05, 2008 2:13 am
by devendra-m
eval — Evaluate a string as PHP code

Re: Function from string

Posted: Tue Aug 05, 2008 2:25 am
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.