You are here: Re: Partial macro expansion of variable and function name. « PHP Programming Language « IT news, forums, messages
Re: Partial macro expansion of variable and function name.

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация