|
Posted by tihu on 05/06/06 00:49
ImOk wrote:
> Aha, the old {} trick. But how does it work with a function name?
>
> E.g.
> $trm="trim";
>
> echo ${'l' . $trm}(" xxxxxx"); // should execute ltrim("
> xxxxxx");
> echo ${'r' . $trm}(" xxxxxx");
>
> The above don't work. What am I doing wrong?
You're doing nothing wrong. If you'd asked an hour ago I would have
said thats the right way to do it so I'm as surprised as you are
I get errors saying the variable named ltrim doesnt exist, then another
error saying the function name should be a string. (Assume the function
name is seen as being null because the variable ltrim doesnt exist).
Maybe the precedence of () is lower than ${}, not really sure...
The next best thing php has is call_user_func..
echo call_user_func( 'l'.$trm , "xxxx" );
I did kinda manage to get a workaround but its not nice. Php is looking
for a variable name ltrim (instead of calling the function ltrim) so
give it what it wants and set the value of $ltrim to 'ltrim'
dynamically. Surprisingly it works..
$trm='trim';
echo ${ ${'l'.$trm} = 'l'.$trm }(' xxxx '); //ewww
Best use call_user_func...
Seeya
Tim
Navigation:
[Reply to this message]
|