473,626 Members | 3,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
..........print s 1000
? &x.tax
............pri nts 8.25

tr="trim"
? &tr.("xxx ")
..........print s 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 3685

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
...........prin ts 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
...........prin ts 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
3231
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 clarification on what 'macro' means. A Lisp macro is a way of modifying code when that code is first defined. It can rearrange the structure of the code, and add and remove parts of it. Unlike C's #define macro language, Lisp macros understand the...
699
33836
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
3
2622
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
3493
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. The Faq pointed me to scpp but i could not compile it. the lex.c generated by flex 2.5.4 is broken.
0
1376
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 is not possible to organize them in distinct files. This would result in cyclic imports. To manage the complexity I implemented some sort of aspect oriented programming (perhaps aspect oriented programming is not quite right in this...
2
2199
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
6
2300
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 the following small example ? #include <stdio.h>
1
3369
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 micro-controllers. So why am I posting herein? Well, the ARMbasic environment makes use of a tool borrowed from your folk's environment - CPP. The build details are: C:\Program Files\Coridium>cpp --version cpp (GCC) 3.2.3 (mingw special 20030504-1) Copyright...
16
2764
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 true. So, could someone look at this code, and help me understand why I get this error. #include <stdio.h>
0
8199
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.