Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How can I display items of expression which is generating from the following PHP code using key and values?

This is my php code for generating a random expression

<?php
function get_random_item($array) { // function to generate operator like(+,-,*,/)  randomly
    $array_length = count($array);
    if ($array_length) {
        return $array[rand(0, $array_length - 1)];
    }
    return null;
}

function generate_random_digit() {
    return rand(1, 9);
}

function generate_random_operand($num_digits) { 
    if($num_digits == 4)
    {
        return rand(1, 999);  
    }
    $operand = "";
    while ($num_digits > 0) {
        $operand = $operand . generate_random_digit();
        if ($operand == "0") {
            $operand = "";
            continue;
        }
        $num_digits--;
    }
    return $operand;
}

function generate_expression($num_operands, $operators, $max_digits = 1) {
    $expression = array();
    while ($num_operands--) {
        $operation = false;
        if (count($expression)) {
            $operation = get_random_item($operators);
            $expression[] = $operation;
        }
        $operand = generate_random_operand($max_digits);

        if ($operation == "/" && $operand == "0") {
            $operand = generate_random_operand($max_digits) + 1;
        }
        $expression[] = $operand;
    }
    return $expression;
}
?>

Now the above code is the logic which is generating random expression i.e random operands & operators

this is code where I am generating expression in output , in the manner that only one item should be visible to user at a time and after clicking next item should be displayed and the current item goes to previous position which will not be vsible to user but I want to show them like this Suppose the expression generated is 2+1=3 then how can i display firstly only 2 then after clicking next button then +1 should be visible to user and the 2 should get in prev position which will not be visible to user and so on till the answer , for this how can I do this with key of that array using foreach loop?

<div class="slide flex" id="out">
    <h3 class="ta-center">Q(<?php echo $index+1 ?>)</h3><br><br>
            <div class="expression flex flex-row" id="exp">
        <?php foreach($expression as$key => $item) { ?>
        <span class="slide-item" id="elements">
            <?php
             if($item == "*"){
                echo "x";
             }else {
             echo $item ?></span><!--displaying each item at a time-->
        <?php }
    } ?>
        <span class="slide-item" id="equal"> = </span>
        <span class="slide-item">
            <span class="answer flex" id="place">
                <span class="answer-placeholder">
                   ****
                </span>

                <span class="answer-content"><!--displaying result-->
                   <?php echo number_format((float)$result, 2, '.', ''); ?>
                </span>

            </span>
        </span>
    </div>

So , how should I do it ?

Comments