Getting a conatant value by name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boolsea
Forum Newbie
Posts: 3
Joined: Tue Aug 08, 2006 11:19 pm
Location: Adelaide Australia

Getting a conatant value by name

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

an amazing function called constant()

Image Image
boolsea
Forum Newbie
Posts: 3
Joined: Tue Aug 08, 2006 11:19 pm
Location: Adelaide Australia

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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;

Code: Select all

constant($dynamicconstant)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
boolsea
Forum Newbie
Posts: 3
Joined: Tue Aug 08, 2006 11:19 pm
Location: Adelaide Australia

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
if ( defined('CONSTANT_NAME') )
{
    echo 'The constant CONSTANT_NAME has a value of ' . CONSTANT_NAME;
}
?>
Post Reply