Page 1 of 1
Getting a conatant value by name
Posted: Tue Aug 08, 2006 11:52 pm
by boolsea
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.
Posted: Tue Aug 08, 2006 11:54 pm
by feyd
an amazing function called
constant()

Posted: Wed Aug 09, 2006 12:28 am
by boolsea
Thanks feyd, this fixed my problem.
The 'constant' function was not in any of my reference books, guess I should have read the php documentation, rather than depend upon 3rd part books.
Posted: Wed Aug 09, 2006 12:31 am
by RobertGonzalez
This is from the manual page on
constant() ...
Code: Select all
<?php
define("MAXSIZE", 100);
echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line
?>
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...
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;
Posted: Wed Aug 09, 2006 12:37 am
by Luke
Everah wrote:This is from the manual page on
constant() ...
Code: Select all
<?php
define("MAXSIZE", 100);
echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line
?>
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...
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;
Posted: Wed Aug 09, 2006 12:43 am
by feyd
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.
Posted: Wed Aug 09, 2006 12:44 am
by boolsea
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.
Posted: Wed Aug 09, 2006 12:46 am
by RobertGonzalez
Code: Select all
<?php
if ( defined('CONSTANT_NAME') )
{
echo 'The constant CONSTANT_NAME has a value of ' . CONSTANT_NAME;
}
?>