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?