• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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?
0
Richard Davis
Asked:
Richard Davis
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
echo "The value of the called constant = " . $$myvar;
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
let me clarify:

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

echo "The value of the called constant = " . $myvar;
returns
"The value of the called constant = this value"
0
 
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.
0
 
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 :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now