Dynamic constant call

Ok, here's the deal

I have a constant defined, hypothetically as such --> define("CONSTANT1", "This value");

In my script, I would like to be able to populate a variable with the name of that constant, i.e.  $myvar = "CONSTANT1";

Now, I would like to be able to refer to the defined value of that constant by calling it up using it's name as stored in $myvar

In otherwords, a form of variable substitution, of sorts.

For instance --> echo "The value of the called constant = " . $myvar; <-- only $myvar is to now somehow directly reference CONSTANT1 as though I had actually called it directly as such --> echo "The value of the called constant = " . CONSTANT1;

Any ideas how I would go about acheiving this or is this not even possible?
LVL 12
Richard DavisSenior Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
echo "The value of the called constant = " . $$myvar;
Guy Hengel [angelIII / a3]Billing EngineerCommented:
let me clarify:

$CONSTANT1 = "this value";
$myvar = "$CONSTANT";

echo "The value of the called constant = " . $myvar;
"The value of the called constant = this value"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard DavisSenior Web DeveloperAuthor Commented:
Hi angelIII,

I'm thinking you slightly misunderstood my question just a tad.

The constant is that of a define() function, so when CONSTANT1 is used in my code as such;

define("CONSTANT1", "This value");
echo "The value of the called constant = " . CONSTANT1;

output would normall equal --> The value of the called constant = This value

this would display the value of the constant's definition...(under normal circumstances), BUT what I am attempting to acheive would be calling my constant by a not so static means.

Basically, what I am working on is writing a template parser script for a template driven website design. The template items that will be compiled with the actual values will be all read from the template html file as {CONSTANT1} and so forth, so I may have a template html file that says,
<TD class="col-norm">{CONSTANT1}</TD>

Now, when I read this file into my template parser class, I am using a preg_match("/\{([^\[]*)\}/", $line_data, $result); command to retrieve the template var...so, template var {CONSTANT1} would be read from the html file, stripped and the string value of "CONSTANT1" would now be stored into an internal variable, $result

Now, I want the string value of $result, which now is equal to a defined constant name --> CONSTANT1, to be accessed and returned just the same as if I were calling it directly like in my given echo example only via a reference or however this could be acheived. Basically the value of the constant will become the replacement value for the html {CONSTANT1}.

Hope this made sense.
Richard DavisSenior Web DeveloperAuthor Commented:
Nevermind angelIII...I figured it out.

Totally simple answer too...can't believe I missed it :S

echo constant($myvar);

this will print out the value assigned to the constant in the define function.

Thanks anyway

I'll give the points just for having responded...I have to anyway...can't leave a question sitting unresolved in here for too long.

Take care & happy coding :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.