473,396 Members | 1,693 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,396 software developers and data experts.

Concatenate string and variable to make up a constant name

Hi,

I know that it is possible to build up a variable name based on a
string and another variable name i.e.

$varnum=5;
${"varnumber_$varnum"} = 'This variable should be called varnumber_5';

but is it possible to do this for a constant? For example I may have
constants set up as

define('CONSTNUMBER_1', 'This is constantnumber 1');
define('CONSTNUMBER_2', 'This is constantnumber 2');
define('CONSTNUMBER_3', 'This is constantnumber 3');

and I may want to access them from inside a loop i.e.

for($counter=1; $counter<=3; $counter}}) {
echo 'Contents of current constant = ' . ${"CONSTNUMBER_$counter"} ;
}

Now obviously this doesn't work because it sees
${"CONSTNUMBER_$counter"} as a variable but if I try and take the $
off then I just get errors.

Can anyone help? Does this even make sense? :-)

Sep 6 '06 #1
1 14740

Peter wrote:
Hi,

I know that it is possible to build up a variable name based on a
string and another variable name i.e.

$varnum=5;
${"varnumber_$varnum"} = 'This variable should be called varnumber_5';

but is it possible to do this for a constant? For example I may have
constants set up as

define('CONSTNUMBER_1', 'This is constantnumber 1');
define('CONSTNUMBER_2', 'This is constantnumber 2');
define('CONSTNUMBER_3', 'This is constantnumber 3');

and I may want to access them from inside a loop i.e.

for($counter=1; $counter<=3; $counter}}) {
echo 'Contents of current constant = ' . ${"CONSTNUMBER_$counter"} ;
}

Now obviously this doesn't work because it sees
${"CONSTNUMBER_$counter"} as a variable but if I try and take the $
off then I just get errors.

Can anyone help? Does this even make sense? :-)
Typically I've just found the answer. I just build up a string of the
constant name and then wrap the constant function around it.

Obviously I was overcomplicating things. Ah well.

Sep 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely....
3
by: Lars Tackmann | last post by:
Hi - I need a function to concatenate a variable number of strings. I have two ideas but cannot deside which way to go. 1) use the "stdarg" macros - if i use these macros it will be easy to step...
35
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
5
by: Generic Usenet Account | last post by:
I have been to recreate a problem that I am having with strings with the trivial code snippet given below. In the trivial code example, I am reading five lines from a data file, each line having...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
4
by: kungfuelmosan | last post by:
Hey guys, Im just getting into c++ at the moment so please bare with me Basically i need to declare a vector<stringstringArray(50) inside a class, however by doing so i am getting the following...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
10
by: Aaron Hoffman | last post by:
Hello, I'm hoping someone might be able to offer some guidance to my problem. I have one query in MS Access which consists of 2 tables joined by a SEQUENCE_ID. By joining the two tables I am...
1
by: mohanprasadgutta | last post by:
hello everyone, i have to write a program which will take a command line arguement which is nothing but a constant name. in the program i am trying to print out the value corresponding to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.