473,385 Members | 1,973 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.

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 14732

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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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,...

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.