473,385 Members | 1,486 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,385 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 3667

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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.