简体   繁体   English

PHP调用常量使用变量

[英]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. 因此,当我在form.php中选择第二个值时,它为$ sort提供了1的值,现在我想使用常量OPTION_1。

Can someone help me? 有人能帮我吗?

Just use constant() function: 只需使用constant()函数:

echo constant('OPTION_' . $sort);

constant() is useful if you need to retrieve the value of a constant, but do not know its name. 如果需要检索常量的值但不知道其名称,则constant()很有用。 Ie it is stored in a variable or returned by a function. 即,它存储在变量中或由函数返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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