469,626 Members | 1,184 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Partial macro expansion of variable and function name.

I come from the Visual Foxpro world, which is one reason I love PHP.
VFP is a scripting type language with macro substitution abilities
similar to PHP.

Besides the regular expansion I can do crazy things (VFP uses & instead
of $):

x="sales"
sales="1000"
salestax="8.25"
? &x
..........prints 1000
? &x.tax
............prints 8.25

tr="trim"
? &tr.("xxx ")
..........prints xxx

? 'This function is &tr.("xxxxx ")'
....... prints This function is trim("xxxxx ")

In VFP the & indicates macro expansions and a space or a . idnicates
the variable termination. Can I do this kind of partial expansion in
PHP? There must be a trick somewhere.

Thanks.

May 5 '06 #1
4 3473

ImOk wrote:
I come from the Visual Foxpro world, which is one reason I love PHP.
VFP is a scripting type language with macro substitution abilities
similar to PHP.

Besides the regular expansion I can do crazy things (VFP uses & instead
of $):

x="sales"
sales="1000"
salestax="8.25"
? &x
.........prints 1000
? &x.tax
...........prints 8.25

tr="trim"
? &tr.("xxx ")
.........prints xxx

? 'This function is &tr.("xxxxx ")'
...... prints This function is trim("xxxxx ")

In VFP the & indicates macro expansions and a space or a . idnicates
the variable termination. Can I do this kind of partial expansion in
PHP? There must be a trick somewhere.

Thanks.


Variable variables might be what you are looking for, they use $$
instead of $

http://www.php.net/manual/en/languag...s.variable.php

Basically

$name = 'joe';
$getvar = 'name'
echo $$getvar; //prints $name so the output is 'joe'

It works with functions and class methods too

$tr = 'trim'
echo $tr(' xxxxx '); // calls the function trim() and prints
xxxx

Seeya

Tim

May 5 '06 #2
ImOk wrote:
I come from the Visual Foxpro world, which is one reason I love PHP.
VFP is a scripting type language with macro substitution abilities
similar to PHP.

Besides the regular expansion I can do crazy things (VFP uses & instead
of $):

x="sales"
sales="1000"
salestax="8.25"
? &x
.........prints 1000
? &x.tax
...........prints 8.25


Just noticed the &$.tax thingy, neat trick..

In php you need to use the dot operator( joins two strings together)
and curly braces ( Think of it like php interprets the contents of {}
as an expression)
http://uk.php.net/manual/en/language...syntax.complex

$x = 'sales'
$sales = 1000;
$salestax = 8.25;

echo $$x; //prints 1000
echo ${$x.'tax'}; //prints 8.25

but
echo $$x.'tax'; // without {} it prints 1000tax

Tim

May 5 '06 #3
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?

May 5 '06 #4

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

May 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by Andrew Dalke | last post: by
699 posts views Thread by mike420 | last post: by
3 posts views Thread by John | last post: by
7 posts views Thread by reppisch | last post: by
reply views Thread by Dr. Peer Griebel | last post: by
2 posts views Thread by talkaboutquality | last post: by
6 posts views Thread by jason | last post: by
16 posts views Thread by mdh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.