简体   繁体   English

PHP使用文本+变量调用常量

[英]PHP calling to a constant using text + variable

I'm creating an application for my mother so she can play quizmaster. 我正在为我的母亲创建一个应用程序,以便她可以玩测验大师。 Since I have to play as on of the canditates, I cannot know the answers + questions. 由于我必须像候选人一样参加比赛,所以我不知道答案和问题。

My solution to this was, creating a constant page so she could fill those out without breaking my code. 我对此的解决方案是,创建一个恒定的页面,以便她可以在不破坏我的代码的情况下填写这些页面。 The display of my page would just be a question with a spoiler tag containing the answer. 我页面的显示只是一个带有包含答案的破坏标签的问题。

Now comes my question, I'm not sure if this is possible, but how do I implement this? 现在是我的问题,我不确定是否可行,但是如何实现呢? I thought about giving an ID to the page (through URL) so I can change the constants to Question_1, Question_2 etc. This way she can add as many questions as she want without me having to make more pages. 我考虑过给页面提供一个ID(通过URL),以便可以将常量更改为Question_1,Question_2等。这样,她可以根据需要添加任意多个问题,而无需创建更多页面。

My page code would look like this: 我的页面代码如下所示:

$page = $_GET['id'];

            <div class="spoiler">

                <input type="button" onclick="showSpoiler(this);" value="Show/Hide" />

                <div class="inner" style="display:none;">
                    <?php echo ANSWER_1; ?>
                </div>

            </div>

Ofcourse, QUESTION . 当然,问题。 $page does not work. $ page不起作用。 (use of undefined constant QUESTION). (使用不确定的常数QUESTION)。 Is it possible to create the application in the way I just described? 是否可以按照我刚才描述的方式创建应用程序?

My bad, I found the solution: 我不好,我找到了解决方案:

just use 只是使用

$constant_name = 'QUESTION' . $page;
echo constant($constant_name);

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

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