Getting a conatant value by name
Moderator: General Moderators
Getting a conatant value by name
Good Morning everyone, I have a very simple problem (that's what they all say)...
I know the name of a constant, and want to get its value (PHP 5.1.4, Apache 2.0.55)
For example:
If I have this in my source:
$name = 'ABC';
And in an included file, i have
define('ABC', 'xxx');
I want to write something like
$value = $$name;
And have $value have a value of 'xxx'.
Please do you know if this is possible?
I know i can get variables using their name, but can't work out how to get constants.
Thanks in advance,
Andrew.
I know the name of a constant, and want to get its value (PHP 5.1.4, Apache 2.0.55)
For example:
If I have this in my source:
$name = 'ABC';
And in an included file, i have
define('ABC', 'xxx');
I want to write something like
$value = $$name;
And have $value have a value of 'xxx'.
Please do you know if this is possible?
I know i can get variables using their name, but can't work out how to get constants.
Thanks in advance,
Andrew.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
This is from the manual page on constant() ...
What is the point of the constant() function if you can get the value of the constant from the contant directly? I'm wondering this actually, this is not a facetious question.
@boolsea: If you know the name of the constant, you can do this...
Code: Select all
<?php
define("MAXSIZE", 100);
echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line
?>@boolsea: If you know the name of the constant, you can do this...
I know the name of a constant, and want to get its value (PHP 5.1.4, Apache 2.0.55)
Code: Select all
$name = 'ABC';
define('ABC', 'xxx');
$value = ABC;Everah wrote:This is from the manual page on constant() ...
What is the point of the constant() function if you can get the value of the constant from the contant directly? I'm wondering this actually, this is not a facetious question.Code: Select all
<?php define("MAXSIZE", 100); echo MAXSIZE; echo constant("MAXSIZE"); // same thing as the previous line ?>
@boolsea: If you know the name of the constant, you can do this...I know the name of a constant, and want to get its value (PHP 5.1.4, Apache 2.0.55)Code: Select all
$name = 'ABC'; define('ABC', 'xxx'); $value = ABC;
Code: Select all
constant($dynamicconstant)- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
for informational and better coding practices I'll add that when I am going to use constant(), I will 99.999999999% of the time use defined() just prior to determine if the constant exists first.
Thanks for your interest gentlemen,
Let me explain what I am doing as this is one use for the constant function.
I work for a mulitnational company, and we have to convert our software texts into multiple languages.
To do this, I define all texts as constants in a file and include the file (or files) into my php code.
At run time, I set the include path based upon the client browsers language preferences, for example, I use 'en-au', so I have an 'en-au' directory, there is also a 'last resort' directory in case we don't support the users language.
This means that when I include the files, I get those that apply to the users language.
This works well if the constant name is known when I write the code.
However, in the latest software we allow our subsidiary to configure the browser pages for the user. Using XML, they define what elements appear in what pages.
Rather than have to write an XML file for each supported language, I want to let them place a constant name in the XML, then I will see if the constant is defined and get its value to put into the html.
Regards, Andrew.
Let me explain what I am doing as this is one use for the constant function.
I work for a mulitnational company, and we have to convert our software texts into multiple languages.
To do this, I define all texts as constants in a file and include the file (or files) into my php code.
At run time, I set the include path based upon the client browsers language preferences, for example, I use 'en-au', so I have an 'en-au' directory, there is also a 'last resort' directory in case we don't support the users language.
This means that when I include the files, I get those that apply to the users language.
This works well if the constant name is known when I write the code.
However, in the latest software we allow our subsidiary to configure the browser pages for the user. Using XML, they define what elements appear in what pages.
Rather than have to write an XML file for each supported language, I want to let them place a constant name in the XML, then I will see if the constant is defined and get its value to put into the html.
Regards, Andrew.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Code: Select all
<?php
if ( defined('CONSTANT_NAME') )
{
echo 'The constant CONSTANT_NAME has a value of ' . CONSTANT_NAME;
}
?>
