create_function, string issue

The following code works


$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');

$a = 2; $b = 3;
echo $newfunc($a, $b) . "\
";

However, I am trying to use a string $x to execute $newfunc but it is not working.
How can I use $x sting in the $newfunc ?


$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');

$x = '2,3';
echo $newfunc($x) . "\
";

Thanks

You’ll need to explode $x into two parts:



 list($a, $b) = explode(',', $x);
$newfunc($a,$b);

However, create_function is generally considered bad practice because it’s diffuclt to debug. Be careful where you use it. In almost all cases you’re better off using closures: http://php.net/manual/en/functions.anonymous.php