Php 如何将变量的值用作常量?
我有一个PHP网站,它使用基于“define”方法的语言系统。 例如:Php 如何将变量的值用作常量?,php,variables,constants,Php,Variables,Constants,我有一个PHP网站,它使用基于“define”方法的语言系统。 例如: define("_question_1", "How old are you?"); define("_question_2", "Question 2?"); define("_question_3", "Question 3?"); .... define("_question_10", "Question 10?"); var_dump(constant('YourConstantNameHere')); 我有5
define("_question_1", "How old are you?");
define("_question_2", "Question 2?");
define("_question_3", "Question 3?");
....
define("_question_10", "Question 10?");
var_dump(constant('YourConstantNameHere'));
SELECT q_title FROM questions_db WHERE id=3 OR id=10;
数据库返回两个文本(!)值:
我将其保存到:
$a = _question_3;
$b = _question_10;
echo ""._question_3.""; //returns "Question 3?"
echo "".$a.""; //returns "_question_3"
但我只有_question_3作为文本值,它是这样工作的:
echo ""._question_3.""; //returns "Question 3?"
echo "".$a.""; //returns "_question_3"
define("_question_3", "Question 3?");
$a = _question_3; //text value
//do something with $a....
echo "".$a.""; //returns "Question 3?"
谢谢你的帮助。
另外,如果不清楚,请更改标题
$a = '_question_3';
$b = 'Question 3?';
$$a = $b;
echo $b;
// prints "Question 3?"
echo $_question_3;
// prints "Question 3?"
$a = '_question_3';
$b = 'Question 3?';
$$a = $b;
echo $b;
// prints "Question 3?"
echo $_question_3;
// prints "Question 3?"
$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'
];
问题
类并创建它的实例
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']);
$a = '_question_3';
$b = 'Question 3?';
$$a = $b;
echo $b;
// prints "Question 3?"
echo $_question_3;
// prints "Question 3?"
$a = '_question_3';
$b = 'Question 3?';
$$a = $b;
echo $b;
// prints "Question 3?"
echo $_question_3;
// prints "Question 3?"
$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'
];
问题
类并创建它的实例
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']);
echo constant($a);
只需使用变量值作为常量,例如
echo constant($a);
Try constant()返回常量的值,例如:
define("_question_1", "How old are you?");
define("_question_2", "Question 2?");
define("_question_3", "Question 3?");
....
define("_question_10", "Question 10?");
var_dump(constant('YourConstantNameHere'));
或者使用变量
var_dump(constant($a));
Try constant()它返回常量的值,例如:
define("_question_1", "How old are you?");
define("_question_2", "Question 2?");
define("_question_3", "Question 3?");
....
define("_question_10", "Question 10?");
var_dump(constant('YourConstantNameHere'));
或者使用变量
var_dump(constant($a));
第二种方法对我不起作用,因为我希望有可能将问题存储在数据库中,并在管理面板中编辑它们。什么原因阻止你这样做?由于第一种方法,我如何知道节省到$b的价值?上面的答案回答了我所需要的!也谢谢你的帮助——我会考虑将来在类似情况下使用关联数组。好的,很高兴你的问题得到了回答。但你做错了。常数不是为此而设计的。你最好找到更合适、更合适的方法来实现你的算法。第二种方法对我不起作用,因为我想在数据库中存储问题并在管理面板中编辑它们。什么原因阻止你这样做?由于第一种方法,我如何知道节省到$b的值是多少?上面的答案回答了我的问题需要!也谢谢你的帮助——我会考虑将来在类似情况下使用关联数组。好的,很高兴你的问题得到了回答。但你做错了。常数不是为此而设计的。你最好找到更合适、更恰当的方法来实现你的算法。令人惊讶!作品这就是我需要的!令人惊讶!作品这就是我需要的!