Dynamic constant call

Posted on 2006-04-09
Last Modified: 2013-12-12
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?
Question by:adrian_brooks
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    echo "The value of the called constant = " . $$myvar;
    LVL 142

    Accepted Solution

    let me clarify:

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

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

    Author Comment

    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, 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.
    LVL 12

    Author Comment

    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 :)

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now