0

Hi I have been tryng to develop an RPN calculator using PHP and at some point in the program a number 1 appears out of nowhere into my array.I have checked the program 3 times with a debugger and I do not understand from where it comes from.Here is my code:

          if(isset($_GET["send"])){
                $v0 = $_GET["val0"];
                $a = explode(" ", $v0);
                $second_array = array();

                function operatii($v , $second_array){
                    $var1 = array_pop($second_array);
                    $var2 = array_pop($second_array);
                    $rez = null;

                    switch ($v){
                        case '+':
                            $rez = $var1 + $var2;
                            break;
                        case '-':
                            $rez = $var2 - $var1;
                            break;
                        case '*':
                            $rez = $var1 * $var2;
                            break;
                        case '/':
                            $rez = $var2 / $var1;
                            break;
                    }

                    array_push($second_array, $rez);
                    print_r($second_array);
                    echo '<br/>';
                }

                for($i = 0; $i < count($a); $i++){
                    if($a[$i] === "+" || $a[$i] === "-" || $a[$i] === "*" || $a[$i] === "/" ){
                        operatii($a[$i] , $second_array);
                        continue;
                    }else{
                        array_push($second_array, $a[$i]);

                    }
                }
            }


        ?>

        <form method="get" action="">
            <input type="text" value="<?php  ?>" name="val1" disabled/>
            <input type="text" value="" name="val0" />
            <input type="submit" value="Introdu" name="send"/>
        </form> 

Long story short this form will take an expressiong like this 5 1 - 5 + and do this operation 5 - 1 + 5.

The problem appears after the for loop runs twice and the operatii() method runs it's sequence , at the end of the method the $rez variable get's pushed into the $second_array. After this the method end's and beetween the operatii method and the start of the next loop iteration 1 get's added into $second_array.

As I mentioned before I runned the debugger 3 times and I can not find any logical explanation as to why and from where that 1 is being added to the array.Can anyone tell what is happening and how can I correct it?

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Nistor Alexandru
  • 5,309
  • 9
  • 46
  • 71

2 Answers2

2

The error is that you are passing $second_array as a value, instead of as a reference. It means that the array gets copied, and any changes you make in operatii function do not affect your second_array outside of function. The thing to do is change

function operatii($v , $second_array){

to

function operatii($v , &$second_array){

More on the topic:

Are arrays in PHP passed by value or by reference?

Community
  • 1
  • 1
Krzysztof Bujniewicz
  • 2,407
  • 21
  • 16
1

You array missing & and also you can save your operators in a array instead

if (isset($_GET["send"])) {
    $_GET["val0"] = isset($_GET["val0"]) ? $_GET["val0"] : "";
    $a = explode(" ", $_GET["val0"]);
    $second_array = array();

    function operatii($v, &$second_array) {
        $var1 = array_pop($second_array);
        $var2 = array_pop($second_array);
        $rez = null;
        switch ($v) {
            case '+' :
                $rez = $var1 + $var2;
                break;
            case '-' :
                $rez = $var2 - $var1;
                break;
            case '*' :
                $rez = $var1 * $var2;
                break;
            case '/' :
                $rez = $var2 / $var1;
                break;
        }
        array_push($second_array, $rez);
        print_r($second_array);
        echo '<br/>';
    }

    $oprators = array("+","-","*","/");
    for($i = 0; $i < count($a); $i ++) {
        if (in_array($a[$i], $oprators)) {
            operatii($a[$i], $second_array);
            continue;
        } else {
            array_push($second_array, $a[$i]);
        }
    }
}
Baba
  • 94,024
  • 28
  • 166
  • 217