Add two numbers

This tutorial will add up two values that have been entered within the web page, I am going to use the web page functional aspects rather than the command line interface because php is mainly used within websites.

The web page itself is using a form tag, this form tag allows for the web page to post data back to the server, where the php will use the posts ($_POST) array ( []).

The source code is

<?php
       $value1 = $_POST['value1'];
       $value2 = $_POST['value2'];
?>
<html>
       <title>PHP - Add two numbers</title>
       <body>
              <form action="addtwonumbers.php" method="post">
                     <input type="text" name="value1" value="0" />
                     <input type="text" name="value2" value="0" />
                     <input type="submit" value="Calculate values"/>
              </form>
              Answer : <?php echo ($value1 + $value2); ?>
       </body>
</html>

if you save that as addtwonumbers.php within the php website configured directory and then open up that page via a web browser (e.g. http://localhost/addtwonumbers.php, localhost means the local pc, just like 127.0.0.1 is the local ring e.g the pc itself to talk to itself via an IP).

Just for completeness, this is the command line interface code.

<?php
 echo "Please enter value 1 : ";
 fscanf(STDIN, "%d\n", $value1); // reads number from STDIN standard input
 echo "Please enter value 2 : ";
 fscanf(STDIN, "%d\n", $value2);
 echo "Answer : " .($value1 + $value2) . "\n";
?>

19 thoughts on “Add two numbers”

  1. Thanks for giving this. Even this simple code will be helpful for me because i am a fresher in programming field..

  2. Notice: Undefined index: value1 in C:\wamp\www\demo\add.php on line 2

    Notice: Undefined index: value2 in C:\wamp\www\demo\add.php on line 3

  3. have not tried out wamp before, but I am guessing this should work from the webpage with wamp

    if (isset($_POST['value1'])
    {
       $value = $_POST['value1'];
    ...
    }

    should work fine.

  4. hii, i am new in php…i want to know if i can add two numbers from two different forms….as i have retrieved the value from db … in form 1, i have value 2 and in 2nd form…i have value 6, now i want to add this two value…how can i do it??

  5. Hi Sidd

    Something like

    <html>
    <body>
    <form name="name1" id="name1" method="POST">
    <input type="text" value="" name="name1t"/>
    <input type="submit" value="submit"/>
        <form name="name2" id="name2" method="POST">
        <input type="text" value="" name="name2t"/>
        <input type="submit" value="submit"/>
        </form>
    </form>
    </body>
    </html>

    Genux

  6. showing like this:-in my program:-
    Undefined index: value1 and value2 in line number like that……….

  7. hii, i am new in php. i want array form with continuesly 2 numbers add in the program to implement by a moodle tool. so i dnt know how?

  8. Hi, I am sorry but not really sure what you are wanting.

    If you are wanting a moodle tool (plugin / module update) etc then yes this can be done.

    but not sure what you are meaning by an array form with “continuously 2 numbers add ?”

  9. if you have php installed and within the “PATH” of the command line interface, then just type

    php

  10. hello i want to add two values from 1 diff input.. and the total will show in total input… heres my code..

    Load:
    Dutty Allow:
    Total:

  11. This tutorial will add up two values that have been entered within the web page, I am going to use the web page functional aspects rather than the command line interface because php is mainly used within websites.

Leave a Reply

Your email address will not be published. Required fields are marked *