473,287 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 3663

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
3
by: John | last post by:
Please, consider this macro: #define mymacro(arg1, arg2) arg1 and arg2 Then it is used: mymacro(boys, girls) How is its expansion?
7
by: reppisch | last post by:
Hi Ng, i am looking for a method of expanding a macro while the rest of the code remains untouched. I have some code which does macro voodo / ifdef's which i would like to strip and simplify. ...
0
by: Dr. Peer Griebel | last post by:
I'm currently writing a small toy application to support symbolic algebra. Therefore I implemented some classes Term, Var, Number, Sum, Product, Power. These classes are tightly coupled. So it...
2
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
6
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
16
by: mdh | last post by:
I have asked a few questions about Macros...and think what I have been missing ...and which all who have replied clearly understand...is that this is a Pre-processor action. Hopefully the above is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.