Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8673427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:27:22+00:00 2026-06-12T19:27:22+00:00

Hi I have been tryng to develop an RPN calculator using PHP and at

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:27:23+00:00Added an answer on June 12, 2026 at 7:27 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to develop a Java ME application using WTK 2.5.2 (Java
I've have been using Mono Develop 2.8 on a to make an ASP MVC
I have been trying to develop some user controls, one being a text edit
I have been trying to develop a peer to peer application that uses Micosoft's
I have been trying to develop a Core Data piece of code and have
I have been trying to setup git for our web development team unsuccessfully. Some
I have been trying to create a ListView which I can sort using drag
I have been trying to make custom radio buttons using HTML, CSS, and JavaScript.
i have been trying to develop to an app that scans for nearby WiFi
I've been trying to develop a code where I have a form to search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.