有没有办法在HTML文档(或PHP脚本)中携带变量?

This is probably more of a 'programming technique' question, but I'm not sure which section of SO that should go to.

I am trying to make a website of "guided questions and (available) answers" - probably easily described structurally like a 'Choose Your Own Adventure' type approach.

Rather than embarking on creating an intricate tree that outlines each and every "storyline branching" and then hard-coding the (a href) to match, I was thinking of a more dynamic "journey" which begins with an array variable that outlines the sequence of the questions that will be asked.

for example, if a user chooses Sequence A; $questionarray = [Q1,Q3,Q2,Q5,Q6] if a user chooses Sequence B; $questionarray = [Q1,Q2,Q4,Q3,Q7] and so on.

My first attempt was to try something simple, just have it in sequence like [Q1,Q2,Q3,Q4,Q5] in which case I wouldn't need an array but a simple incrementing variable, say $qnum, and each subsequent file would call the next, like so - Q1.htm calls Q2.htm, Q2.htm calls Q3.htm, but not because it has been hard-coded in each file but determined by the incrementing 'unifying variable' of $qnum. (the thought was that, if that succeeded, it would then be a simple case of replacing $qnum with the afore-mentioned array variable '$questionarray'.)

The problem then arose that over several documents, HTML or even PHP, $qnum couldn't be "carried across" to the next file - without writing to, say, a textfile on the server-side to keep track.

I even tried to change the above 'sibling call sibling' structure to 'parent call child' (via "include Q".$qnum.".php") but the $_POST/document.reload also ends up resetting the $qnum back to declared value.

Is there another method I could use that I'm not aware of - aside from writing to/reading from a server-side text file, whichh would be quite inefficient, right?

In Php you can use Sessions to keep track of data from one request to the next.

you will need to start a session first and then you can get/set data in the $_SESSION array.

For example:

On the first page

<?php

session_start();

$_SESSION['questions'] = array(1,2,4,3,7);

?>

On a secondary page

<?php

session_start();

//Replace with whatever logic is needed
foreach($_SESSION['questions'] as $q){ 
  echo $q   
}

Note you should only start a session once per request. To check if a session is already started it is recommended you do the following:

if (session_status() == PHP_SESSION_NONE) {
  session_start();
}

If you need to delete data from the session you can go about it in two ways:

To delete an index in the $_SESSION array

unset($_SESSION['questions']);

To destroy the entire session

session_destroy();

If you need to carry variables across webpages, Sessions are the way to go. Sessions allow you to pass variables, arrays, and the like across multiple pages. Check here for an into to Sessions.