找到你要的答案

Q:How can I use the value of a variable as constant?

Q:如何使用变量的值为常数?

  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?");
    
  2. 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;
    

    Database returns two TEXT(!) values:

    _question_3, _question_10
    

    Which I save to:

    $a = _question_3; 
    $b = _question_10;
    
  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?"
    

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

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

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

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

  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?");
    
  2. 我有5-10个问题我要问。我需要选择我想问的问题。所以我对我的数据库做了一个查询。像:

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

    数据库返回两个文本(!)价值观:

    _question_3, _question_10
    

    我保存到:

    $a = _question_3; 
    $b = _question_10;
    
  3. 接下来我需要的是显示前面描述的描述文本。当我用_question_3变量是这样的:

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

    但我只有_question_3作为文本的价值和它的作品是这样的:

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

问题:如何将文本值的PHP变量和做点什么吗?

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

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

answer1: 回答1:

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

echo constant($a);

只是用constant()使用变量的值为常数,例如

echo constant($a);
answer2: 回答2:
  1. In PHP you could use variable variable – dynamic variable names.

    $a = '_question_3'; 
    $b = 'Question 3?';
    
    $$a = $b;
    echo $b;
    // prints "Question 3?"
    echo $_question_3;
    // prints "Question 3?"
    

But it's not good solution. Try not to use dynamic variable names.

  1. May be associative array will be more suitable to you?

    $questions = []; // Creating an empty array
    $questions['_question_3']['question'] = 'Question 3?'; // Storing a question
    $questions['_question_3']['answers'] = [ // Adding answers
        'Answer 1',
        'Answer 2',
        'Answer 3'
    ];
    
  2. Or declare a Question class and create instances of it.

    class Question
    {
      public $Question;
    
      public $Answers;
    
      __construct($question, $answers)
      {
         $this->Question = $question;
         $this->Answers = $answers;
      }
    }
    
    $q1 = new Question('Question 1?', ['Answer 1', 'Answer 2', 'Answer 3']);
    
  1. 在PHP中可以使用变量–动态变量名。

    $a = '_question_3'; 
    $b = 'Question 3?';
    
    $$a = $b;
    echo $b;
    // prints "Question 3?"
    echo $_question_3;
    // prints "Question 3?"
    

但这不是很好的解决办法。尽量不要使用动态变量名。

  1. 可能是联想数组会更适合你吗?

    $questions = []; // Creating an empty array
    $questions['_question_3']['question'] = 'Question 3?'; // Storing a question
    $questions['_question_3']['answers'] = [ // Adding answers
        'Answer 1',
        'Answer 2',
        'Answer 3'
    ];
    
  2. 或声明一个问题类并创建它的实例。

    class Question
    {
      public $Question;
    
      public $Answers;
    
      __construct($question, $answers)
      {
         $this->Question = $question;
         $this->Answers = $answers;
      }
    }
    
    $q1 = new Question('Question 1?', ['Answer 1', 'Answer 2', 'Answer 3']);
    
answer3: 回答3:

Try constant() it returns the value of a constant, for example:

var_dump(constant('YourConstantNameHere'));

or with variables

var_dump(constant($a));

http://www.php.net/manual/en/function.constant.php

试着constant()返回值恒定,例如:

var_dump(constant('YourConstantNameHere'));

或变量

var_dump(constant($a));

http://www.php.net/manual/en/function.constant.php

php  variables  constants