简体   繁体   中英

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.

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. (use of undefined constant 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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