如何将变量的值用作常量

如何将变量的值用作常量

本文介绍了如何将变量的值用作常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我有一个PHP站点,该站点使用基于定义方法的语言系统。
    例如:

  1. I have a PHP site which uses language system based on "define" method.For example:

define("_question_1", "How old are you?");
define("_question_2", "Question 2?");
define("_question_3", "Question 3?");
....
define("_question_10", "Question 10?");


  • 我要问5-10个问题。我需要选择我要问的问题。因此,我向数据库进行查询。像这样的东西:

  • I have 5-10 questions I need to ask. And I need to select what question I want to ask. So I make a query to my DB. Something like:

    SELECT q_title FROM questions_db WHERE id=3 OR id=10;
    

    数据库返回两个TEXT(!)值:

    Database returns two TEXT(!) values:

    _question_3, _question_10
    

    我保存到:

    $a = _question_3;
    $b = _question_10;
    


  • 下一步,我需要显示前面定义的描述文本。当我将_question_3用作变量时,它的工作方式如下:

  • Next thing I need is to show the text of the description defined earlier. When I use _question_3 as VARIABLE it works like this:

    echo ""._question_3.""; //returns "Question 3?"
    

    但是我只有_question_3作为TEXT值,它的工作方式如下:

    But I have only _question_3 as TEXT value and it's works like this:

    echo "".$a.""; //returns "_question_3"
    


  • 问题:如何将文本值转换为PHP变量并进行类似的操作?

    THE QUESTION: How to convert text value to PHP variable and make something like?

        define("_question_3", "Question 3?");
        $a = _question_3; //text value
        //do something with $a....
        echo "".$a.""; //returns "Question 3?"
    

    感谢您的帮助。
    P.S.如果不清楚,请更改标题。

    Thanks for help.P.S. Please change the title if it doesn't clear to understanding.

    推荐答案

    只需使用以将变量值用作常量,例如

    Just use constant() to use your variable value as constant, e.g.

    echo constant($a);
    

    这篇关于如何将变量的值用作常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-28 00:12