简体   繁体   中英

php call constant with using a variable

//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?

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. Ie it is stored in a variable or returned by a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM