How can the complexity of decision trees in a multi-page form be streamlined for easier maintenance in PHP?
Issue: The complexity of decision trees in a multi-page form can be streamlined for easier maintenance in PHP by using arrays to store the decision tree structure. This allows for easier modification and maintenance of the decision tree logic. PHP Code Snippet:
// Define the decision tree structure using arrays
$decisionTree = [
'question' => 'Is the user a new customer?',
'yes' => [
'question' => 'Is the user interested in product A?',
'yes' => 'Show page 1',
'no' => 'Show page 2'
],
'no' => 'Show page 3'
];
// Function to traverse the decision tree based on user responses
function traverseDecisionTree($decisionTree) {
while (is_array($decisionTree)) {
echo $decisionTree['question'] . "\n";
$response = readline("Enter 'yes' or 'no': ");
$decisionTree = $decisionTree[$response];
}
echo $decisionTree . "\n";
}
// Call the function to traverse the decision tree
traverseDecisionTree($decisionTree);