create_function, string issue

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
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

create_function, string issue

Post by pedroz »

The following code works

Code: Select all

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

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

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 ?

Code: Select all

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

$x = '2,3';
echo $newfunc($x) . "\n";
Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: create_function, string issue

Post by Christopher »

You can't because the function requires two parameters. $x is just a string that happens to have a comma in it. But PHP does not look inside strings like that.
(#10850)
Post Reply