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 8314917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:59:11+00:00 2026-06-08T20:59:11+00:00

This calculator is supposed to take a typed input with spaces, (like 2 +

  • 0

This calculator is supposed to take a typed input with spaces, (like “2 + 2”) in the web page text box, and then echo the answer above it. Currently, the output is either “0”, or it will seemingly clip a random number off of the input.

I’m 95% sure the problem is within the substr functions – they are not being assigned the correct values. This is my guess because of the output behavior. To demonstrate,

1 + 1 will = 0
2 + 2 will = 0
…
9 + 9 will = 0
10 + 10 will = 1
20 + 20 will = 2

note: The $firstNumber, $secondNumber, and $operator initial declaration values starting on line 16 are arbitrary.

Here are substr( , ) examples from PHP manual:

echo substr('abcdef', 1);     // bcdef   <br>
echo substr('abcdef', 1, 3);  // bcd<br>
echo substr('abcdef', 0, 4);  // abcd<br>
echo substr('abcdef', 0, 8);  // abcdef<br>
echo substr('abcdef', -1, 1); // f<br>

.

//'if' is executed when submit is pressed
if (isset($_POST['test']))
{
    $input = $_POST['test'];
    $answer = calculate($input);
    echo $answer;
}

//does string processing and number calculation
function calculate($input)
{
    //declarations (random)
    $firstNumber = 20;
    $secondNumber = 30;
    $operator = "+";
    $answer = 7;

    //string processing. 
    for($i = 0; $i <= strlen($input); $i++)  
    {
        //if current position of the string scan is an operator,
        if( trim(substr($input, $i, $i + 1), " ") == "+" ||trim(substr($input, $i, $i + 1), " ") == "-"
          ||trim(substr($input, $i, $i + 1), " ") == "*" ||trim(substr($input, $i, $i + 1), " ") == "/")
        {
            //then 

            //$operator = current position TO current position + 1
            $operator = substr($input, $i, $i + 1);
            //trim $operator
            $operator = trim($operator, " ");

            //$firstNumber = 0 TO current position - 1
            $firstNumber = substr($input, 0, $i - 1);
            //trim $operator
            $firstNumber = trim($firstNumber, " ");

            //$secondNumber = current position + 1 TO end of string
            $secondNumber = substr($input, $i + 1, strlen($input));
            //trim $operator
            $secondNumber = trim($secondNumber, " ");
        }

    }

    //if operator is ... then do that operation. 
    //example: if "+", then add $firstNumber and $secondNumber

    if($operator == "+")
    {
        $answer = $firstNumber + $secondNumber;
    }
    else if($operator == "-")
    {
        $answer = $firstNumber - $secondNumber;
    }
    else if($operator == "*")
    {
        $answer = $firstNumber * $secondNumber;
    }
    else if($operator == "/")
    {
        $answer = $firstNumber / $secondNumber;
    }

    //return the calculated answer for echo
    return $answer;
}

?>

<form action="test.php" method="POST">

    <textarea name="test" rows="3" cols="30"></textarea>
    <input type="submit" value="calculate!">

</form>
  • 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-08T20:59:13+00:00Added an answer on June 8, 2026 at 8:59 pm

    Your problem is your use of substr.

    substr take a string, a start position and a length. It’s the length that you are misusing. As you pan through the input string, you are increasing the length of the substring that you are looking for. Since operators have length of 1, you should be using

        //if current position of the string scan is an operator,
        if( trim(substr($input, $i, 1), " ") == "+" || trim(substr($input, $i, 1), " ") == "-"
          ||trim(substr($input, $i, 1), " ") == "*" || trim(substr($input, $i, 1), " ") == "/")
        {
             ....
    

    You also need to update your code here:

    //$operator = current position TO current position + 1
    $operator = substr($input, $i, 1);
    //trim $operator
    $operator = trim($operator, " ");
    

    You could simplify this a little by doing

      potentialOperator = trim(substr($input, $i, 1), " ")
    

    and comparing that to your supported operators. That’ll save you multiple unnecessary calls to trim and substr
    It’ll also mean that you identify the operator once and once only.

    You might also want to look at preg_split with PREG_SPLIT_DELIM_CAPTURE for parsing the input string, instead of scanning it char by char.

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

Sidebar

Related Questions

So I have this calculator I'm building that accepts user variable inputs like let
so I'm deploying a shipping calculator using this function , and I'm getting this
My main class displays a JMenuBar. This JMenuBar is managed from calculator.ui.MenuBar. public JMenuBar
This function is from the book Programming Collective Intelligence, and is supposed to calculate
My program is supposed to read an integer n from the user and then
This is a program that is supposed to prompt user for hours worked and
I have a strange problem with a calculator made using boost::spirit. This calculator is
I have a table that seems like this: +-----+-----------+------------+ | id | value |
So my question is on this: This program is supposed to calculate the area
I don't know if this is even possible but I'd like to be able

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.