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";
?>

Tags: , , , ,

8 Responses to “Add two numbers”

  1. sreeraj says:

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

  2. mizanur rahman says:

    thank you for giving the example

  3. Jc fel says:

    Thanks for this… But, how about if the input is coming from an html textbox? Do i have to use fscanf function?

  4. genux says:

    fscanf is more for files than HTML code.

  5. subodh says:

    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

  6. genux says:

    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.

  7. sidd says:

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

  8. genux says:

    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

Leave a Reply