0
//include.php
    define('OPTION_0', 'Essence of population');
    define('OPTION_1', 'Passport request/extend');
    define('OPTION_2', 'Request logging concession');

//form.php
    <select name="sort">
        <option value="0"><?php echo(O_0) ?></option>
        <option value="1"><?php echo(O_1) ?></option>
        <option value="2"><?php echo(O_2 ?></option>
    </select>


//show.php
    extract($_POST);                    //The variable $sort has the value 1,2 or 3
    echo("This is your choice");
    echo(OPTION_ . $sort);              //I want to use de constant e.g. OPTION_2

I want to echo the value of the matching constant. So when I select the second value in form.php it gives $sort the value of 1 now I want to use the constants OPTION_1.

Can someone help me?

1
  • sort of voids the purpose of constants Commented Feb 18, 2014 at 8:25

1 Answer 1

7

Just use constant() function:

echo constant('OPTION_' . $sort);

constant() is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.